@@ -5,17 +5,17 @@ permalink: /docs/handbook/release-notes/typescript-4-0.html
55oneline : TypeScript 4.0 Release Notes
66---
77
8- ## Variadic Tuple Types
8+ ## ๊ฐ๋ณ ์ธ์ ํํ ํ์
( Variadic Tuple Types)
99
10- Consider a function in JavaScript called ` concat ` that takes two array or tuple types and concatenates them together to make a new array .
10+ ๋ฐฐ์ด์ด๋ ํํ ํ์
๋ ๊ฐ๋ฅผ ๊ฒฐํฉํ์ฌ ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋๋ JavaScript์ ` concat ` ํจ์์ ๋ํด์ ์๊ฐํด๋ด
์๋ค .
1111
1212``` js
1313function concat (arr1 , arr2 ) {
1414 return [... arr1, ... arr2];
1515}
1616```
1717
18- Also consider ` tail ` , that takes an array or tuple, and returns all elements but the first .
18+ ๊ทธ๋ฆฌ๊ณ , ๋ฐฐ์ด์ด๋ ํํ์ ๋ณ์๋ก ์
๋ ฅ๋ฐ์ ์ฒซ ๋ฒ์งธ ์์๋ฅผ ์ ์ธํ ๋๋จธ์ง๋ฅผ ๋ฐํํ๋ ` tail ` ํจ์์ ๋ํด์๋ ์๊ฐํด๋ด
์๋ค .
1919
2020``` js
2121function tail (arg ) {
@@ -24,9 +24,9 @@ function tail(arg) {
2424}
2525```
2626
27- How would we type either of these in TypeScript ?
27+ TypeScript์์๋ ์ด ๋ ํจ์์ ํ์
์ ์ด๋ป๊ฒ ์ ์ํ ์ ์์๊น์ ?
2828
29- For ` concat ` , the only valid thing we could do in older versions of the language was to try and write some overloads .
29+ ` concat ` ์ ๊ฒฝ์ฐ, ์ด์ ๋ฒ์ ์์๋ ์ฌ๋ฌ ๊ฐ์ ์ค๋ฒ๋ก๋๋ฅผ ์์ฑํ๋ ๋ฐฉ๋ฒ์ด ์ ์ผํ์ต๋๋ค .
3030
3131``` ts
3232function concat(arr1 : [], arr2 : []): [];
@@ -38,8 +38,8 @@ function concat<A, B, C, D, E>(arr1: [A, B, C, D, E], arr2: []): [A, B, C, D, E]
3838function concat<A , B , C , D , E , F >(arr1 : [A , B , C , D , E , F ], arr2 : []): [A , B , C , D , E , F ];)
3939```
4040
41- Uh ...okay, that's...seven overloads for when the second array is always empty .
42- Let's add some for when ` arr2 ` has one argument .
41+ ์ ... ๋ค, ์ด ์ค๋ฒ๋ก๋๋ค์ ๋ ๋ฒ์งธ ๋ฐฐ์ด์ ์ ๋ถ ๋น์ด์์ต๋๋ค .
42+ ์ด๋, ` arr2 ` ๊ฐ ํ๋์ ์ธ์๋ฅผ ๊ฐ์ง๊ณ ์๋ ๊ฒฝ์ฐ๋ฅผ ์ถ๊ฐํด๋ด
์๋ค .
4343
4444<!-- prettier-ignore -->
4545``` ts
@@ -52,26 +52,26 @@ function concat<A1, B1, C1, D1, E1, A2>(arr1: [A1, B1, C1, D1, E1], arr2: [A2]):
5252function concat<A1 , B1 , C1 , D1 , E1 , F1 , A2 >(arr1 : [A1 , B1 , C1 , D1 , E1 , F1 ], arr2 : [A2 ]): [A1 , B1 , C1 , D1 , E1 , F1 , A2 ];
5353```
5454
55- We hope it's clear that this is getting unreasonable .
56- Unfortunately, you'd also end up with the same sorts of issues typing a function like ` tail ` .
55+ ์ด๋ฐ ์ค๋ฒ๋ก๋ฉ ํจ์๋ค์ ๋ถ๋ช
๋นํฉ๋ฆฌ์ ์
๋๋ค .
56+ ์ํ๊น๊ฒ๋, ` tail ` ํจ์๋ฅผ ํ์ดํํ ๋๋ ์ด์ ๋น์ทํ ๋ฌธ์ ์ ์ง๋ฉดํ๊ฒ ๋ฉ๋๋ค .
5757
58- This is another case of what we like to call " death by a thousand overloads", and it doesn't even solve the problem generally .
59- It only gives correct types for as many overloads as we care to write .
60- If we wanted to make a catch-all case, we'd need an overload like the following:
58+ ์ด๊ฒ์ "์ฒ ๊ฐ์ ์ค๋ฒ๋ก๋๋ก ์ธํ ์ฃฝ์( death by a thousand overloads)"์ ํ๋์ ๊ฒฝ์ฐ์ด๋ฉฐ, ์ฌ์ง์ด ๋๋ถ๋ถ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ง๋ ๋ชปํฉ๋๋ค .
59+ ์ฐ๋ฆฌ๊ฐ ์์ฑํ๊ณ ์ ํ๋ ๋งํผ์ ์ค๋ฒ๋ก๋์ ํํด์๋ง ์ฌ๋ฐ๋ฅธ ํ์
์ ์ ๊ณตํฉ๋๋ค .
60+ ํฌ๊ด์ ์ธ ์ผ์ด์ค๋ฅผ ๋ง๋ค๊ณ ์ถ๋ค๋ฉด, ๋ค์๊ณผ ๊ฐ์ ์ค๋ฒ๋ก๋๊ฐ ํ์ํฉ๋๋ค.
6161
6262``` ts
6363function concat<T , U >(arr1 : T [], arr2 : U []): Array <T | U >;
6464```
6565
66- But that signature doesn't encode anything about the lengths of the input, or the order of the elements, when using tuples .
66+ ๊ทธ๋ฌ๋ ์ ์๊ทธ๋์ฒ๋ ํํ์ ์ฌ์ฉํ ๋ ์
๋ ฅ ๊ธธ์ด๋ ์์ ์์์ ๋ํ ์ด๋ค ๊ฒ๋ ์ฒ๋ฆฌํ์ง ์์ต๋๋ค .
6767
68- TypeScript 4.0 brings two fundamental changes, along with inference improvements, to make typing these possible .
68+ TypeScript 4.0์ ํ์
์ถ๋ก ๊ฐ์ ์ ํฌํจํ ๋ ๊ฐ์ง ํต์ฌ์ ์ธ ๋ณํ๋ฅผ ๋์
ํด ์ด๋ฌํ ํ์ดํ์ ๊ฐ๋ฅํ๋๋ก ๋ง๋ค์์ต๋๋ค .
6969
70- The first change is that spreads in tuple type syntax can now be generic .
71- This means that we can represent higher-order operations on tuples and arrays even when we don't know the actual types we're operating over .
72- When generic spreads are instantiated (or, replaced with a real type) in these tuple types, they can produce other sets of array and tuple types .
70+ ์ฒซ ๋ฒ์งธ ๋ณํ๋ ํํ ํ์
๊ตฌ๋ฌธ์ ์คํ๋ ๋ ์ฐ์ฐ์์์ ์ ๋ค๋ฆญ ํ์
์ ์ฌ์ฉํ ์ ์๋ค๋ ์ ์
๋๋ค .
71+ ์ฐ๋ฆฌ๊ฐ ์๋ํ๋ ์ค์ ํ์
์ ๋ชจ๋ฅด๋๋ผ๋ ํํ๊ณผ ๋ฐฐ์ด์ ๋ํ ๊ณ ์ฐจํจ์๋ฅผ ํํํ ์ ์๋ค๋ ๋ป์
๋๋ค .
72+ ์ด๋ฌํ ํํ ํ์
์์ ์ ๋ค๋ฆญ ์คํ๋ ๋ ์ฐ์ฐ์๊ฐ ์ธ์คํด์คํ(ํน์, ์ค์ ํ์
์ผ๋ก ๋์ฒด)๋๋ฉด ๋ ๋ค๋ฅธ ๋ฐฐ์ด์ด๋ ํํ ํ์
์ธํธ๋ฅผ ์์ฐํ ์ ์์ต๋๋ค .
7373
74- For example, that means we can type function like ` tail ` , without our " death by a thousand overloads" issue .
74+ ์๋ฅผ ๋ค์ด, ` tail ` ๊ฐ์ ํจ์๋ฅผ "์ฒ ๊ฐ์ ์ค๋ฒ๋ก๋๋ก ์ธํ ์ฃฝ์( death by a thousand overloads)"์ด์ ์์ด ํ์ดํ ํ ์ ์๊ฒ ๋ฉ๋๋ค .
7575
7676``` ts
7777function tail<T extends any []>(arr : readonly [any , ... T ]) {
@@ -83,41 +83,41 @@ const myTuple = [1, 2, 3, 4] as const;
8383const myArray = [" hello" , " world" ];
8484
8585const r1 = tail (myTuple );
86- // ^?
86+ // ^ = const r1: [2, 3, 4]
8787
8888const r2 = tail ([... myTuple , ... myArray ] as const );
89- // ^?
89+ // ^ = const r2: [2, 3, 4, ...string[]]
9090```
9191
92- The second change is that rest elements can occur anywhere in a tuple - not just at the end!
92+ ๋ ๋ฒ์งธ ๋ณํ๋ ๋๋จธ์ง ์์๊ฐ ๋๋ฟ๋ง ์๋๋ผ ํํ์ ์ด๋ ๊ณณ์์๋ ๋ฐ์ํ ์ ์๋ค๋ ๊ฒ์
๋๋ค.
9393
9494``` ts
9595type Strings = [string , string ];
9696type Numbers = [number , number ];
9797
9898type StrStrNumNumBool = [... Strings , ... Numbers , boolean ];
99- // ^?
99+ // ^ = type StrStrNumNumBool = [string, string, number, number, boolean]
100100```
101101
102- Previously, TypeScript would issue an error like the following :
102+ ์ด์ ์๋, TypeScript๋ ๋ค์๊ณผ ๊ฐ์ ์ค๋ฅ๋ฅผ ์์ฑํ์์ต๋๋ค :
103103
104104```
105105A rest element must be last in a tuple type.
106106```
107107
108- But with TypeScript 4.0, this restriction is relaxed .
108+ TypeScript 4.0์์๋ ์ด๋ฌํ ์ ํ์ด ์ํ๋์์ต๋๋ค .
109109
110- Note that in cases when we spread in a type without a known length, the resulting type becomes unbounded as well, and all the following elements factor into the resulting rest element type .
110+ ๊ธธ์ด๊ฐ ์ ํด์ง์ง ์์ ํ์
์ ํ์ฅํ๋ ค๊ณ ํ ๋, ๊ฒฐ๊ณผ์ ํ์
์ ์ ํ๋์ง ์์ผ๋ฉฐ, ๋ค์ ๋ชจ๋ ์์๊ฐ ๊ฒฐ๊ณผ์ ๋๋จธ์ง ์์ ํ์
์ ํฌํจ๋๋ ์ ์ ์ ์ํ์๊ธฐ ๋ฐ๋๋๋ค .
111111
112112``` ts
113113type Strings = [string , string ];
114114type Numbers = number [];
115115
116116type Unbounded = [... Strings , ... Numbers , boolean ];
117- // ^?
117+ // ^ = type Unbounded = [string, string, ...(number | boolean)[]]
118118```
119119
120- By combining both of these behaviors together, we can write a single well-typed signature for ` concat ` :
120+ ์ด ๋ ๊ฐ์ง ๋์์ ํจ๊ป ๊ฒฐํฉํ์ฌ, ` concat ` ์ ๋ํด ํ์
์ด ์ ๋๋ก ์ ์๋ ์๊ทธ๋์ฒ๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.
121121
122122``` ts
123123type Arr = readonly any [];
@@ -127,20 +127,20 @@ function concat<T extends Arr, U extends Arr>(arr1: T, arr2: U): [...T, ...U] {
127127}
128128```
129129
130- While that one signature is still a bit lengthy, it's just one signature that doesn't have to be repeated, and it gives predictable behavior on all arrays and tuples .
130+ ํ๋์ ์๊ทธ๋์ฒ๊ฐ ์กฐ๊ธ ๊ธธ๋๋ผ๋, ๋ฐ๋ณตํ ํ์๊ฐ ์๋ ํ๋์ ์๊ทธ๋์ฒ์ผ ๋ฟ์ด๋ฉฐ, ๋ชจ๋ ๋ฐฐ์ด๊ณผ ํํ์์ ์์ธก ๊ฐ๋ฅํ ํ๋์ ์ ๊ณตํฉ๋๋ค .
131131
132- This functionality on its own is great, but it shines in more sophisticated scenarios too .
133- For example, consider a function to [ partially apply arguments ] ( https://en.wikipedia.org/wiki/Partial_application ) called ` partialCall ` .
134- ` partialCall ` takes a function - let's call it ` f ` - along with the initial few arguments that ` f ` expects .
135- It then returns a new function that takes any other arguments that ` f ` still needs, and calls ` f ` when it receives them .
132+ ์ด ๊ธฐ๋ฅ์ ๊ทธ ์์ฒด๋ง์ผ๋ก๋ ํ๋ฅญํ์ง๋ง, ์กฐ๊ธ ๋ ์ ๊ตํ ์๋๋ฆฌ์ค์์๋ ๋น์ ๋ฐํฉ๋๋ค .
133+ ์๋ฅผ ๋ค์ด, [ ํจ์์ ๋งค๊ฐ๋ณ์๋ฅผ ๋ถ๋ถ์ ์ผ๋ก ์ ์ฉํ์ฌ ์๋ก์ด ํจ์๋ฅผ ๋ฐํํ๋ ] ( https://en.wikipedia.org/wiki/Partial_application ) ` partialCall ` ํจ์๊ฐ ์๋ค๊ณ ์๊ฐํด๋ด
์๋ค .
134+ ` partialCall ` ์ ๋ค์๊ณผ ๊ฐ์ ํจ์๋ฅผ ๊ฐ์ง๋๋ค. - ` f ` ๊ฐ ์์ํ๋ ๋ช ๊ฐ์ง ์ธ์์ ํจ๊ป ` f ` ๋ผ๊ณ ์ง์ ํ๊ฒ ์ต๋๋ค .
135+ ๊ทธ ํ, ` f ` ๊ฐ ์ฌ์ ํ ํ์๋ก ํ๋ ๋ค๋ฅธ ์ธ์๋ฅผ ๊ฐ์ง๊ณ , ๊ทธ๊ฒ์ ๋ฐ์ ๋ ` f ` ๋ฅผ ํธ์ถํ๋ ์๋ก์ด ํจ์๋ฅผ ๋ฐํํฉ๋๋ค .
136136
137137``` js
138138function partialCall (f , ... headArgs ) {
139139 return (... tailArgs ) => f (... headArgs, ... tailArgs);
140140}
141141```
142142
143- TypeScript 4.0 improves the inference process for rest parameters and rest tuple elements so that we can type this and have it "just work" .
143+ TypeScript 4.0์ ๋๋จธ์ง ํ๋ผ๋ฏธํฐ๋ค๊ณผ ํํ ์์๋ค์ ๋ํ ์ถ๋ก ํ๋ก์ธ์ค๋ฅผ ๊ฐ์ ํ์ฌ ํ์
์ ์ง์ ํ ์ ์๊ณ "๊ทธ๋ฅ ๋์"ํ๋๋ก ํ ์ ์์ต๋๋ค .
144144
145145``` ts
146146type Arr = readonly unknown [];
@@ -153,7 +153,7 @@ function partialCall<T extends Arr, U extends Arr, R>(
153153}
154154```
155155
156- In this case , ` partialCall ` understands which parameters it can and can't initially take, and returns functions that appropriately accept and reject anything left over .
156+ ์ด ๊ฒฝ์ฐ , ` partialCall ` ์ ์ฒ์์ ์ทจํ ์ ์๋ ํ๋ผ๋ฏธํฐ์ ํ ์ ์๋ ํ๋ผ๋ฏธํฐ๋ฅผ ํ์
ํ๊ณ , ๋จ์ ๊ฒ๋ค์ ์ ์ ํ ์์ฉํ๊ณ ๊ฑฐ๋ถํ๋ ํจ์๋ค์ ๋ฐํํฉ๋๋ค .
157157
158158``` ts
159159// @errors: 2345 2554 2554 2345
@@ -172,23 +172,23 @@ const f1 = partialCall(foo, 100);
172172
173173const f2 = partialCall (foo , " hello" , 100 , true , " oops" );
174174
175- // This works !
175+ // ์๋ํฉ๋๋ค !
176176const f3 = partialCall (foo , " hello" );
177- // ^?
177+ // ^ = const f3: (y: number, z: boolean) => void
178178
179- // What can we do with f3 now ?
179+ // f3์ผ๋ก ๋ญ ํ ์ ์์๊น์ ?
180180
181- // Works !
181+ // ์๋ํฉ๋๋ค !
182182f3 (123 , true );
183183
184184f3 ();
185185
186186f3 (123 , " hello" );
187187```
188188
189- Variadic tuple types enable a lot of new exciting patterns, especially around function composition .
190- We expect we may be able to leverage it to do a better job type-checking JavaScript's built-in ` bind ` method .
191- A handful of other inference improvements and patterns also went into this, and if you're interested in learning more, you can take a look at [ the pull request] ( https://github.com/microsoft/TypeScript/pull/39094 ) for variadic tuples .
189+ ๊ฐ๋ณ ์ธ์ ํํ ํ์
์ ํนํ ๊ธฐ๋ฅ ๊ตฌ์ฑ๊ณผ ๊ด๋ จํ์ฌ ๋ง์ ์๋ก์ด ํฅ๋ฏธ๋ก์ด ํจํด์ ๊ฐ๋ฅํ๊ฒ ํฉ๋๋ค .
190+ ์ฐ๋ฆฌ๋ JavaScript์ ๋ด์ฅ๋ ` bind ` ๋ฉ์๋์ ํ์
์ฒดํน์ ๋ ์ํ๊ธฐ ์ํด ์ด๋ฅผ ํ์ฉํ ์ ์์ ๊ฒ์ด๋ผ๊ณ ๊ธฐ๋ํฉ๋๋ค .
191+ ๋ช ๊ฐ์ง ๋ค๋ฅธ ์ถ๋ก ๊ฐ์ ๋ฐ ํจํด๋ค๋ ์ฌ๊ธฐ์ ํฌํจ๋์ด ์์ผ๋ฉฐ, ๊ฐ๋ณ ์ธ์ ํํ์ ๋ํด ๋ ์์๋ณด๊ณ ์ถ๋ค๋ฉด, [ the pull request] ( https://github.com/microsoft/TypeScript/pull/39094 ) ๋ฅผ ์ฐธ๊ณ ํด๋ณด์ธ์ .
192192
193193## Labeled Tuple Elements
194194
0 commit comments