You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/Tuple Kinds.md
+35-37
Original file line number
Diff line number
Diff line change
@@ -2,18 +2,23 @@
2
2
3
3
## Give Specific Types to Variadic Functions
4
4
5
-
This proposal lets Typescript users type higher-order functions that take a variable number of parameters.
6
-
Functions like this include any `concat`, `apply`, `curry`, `compose`/`pipe` and almost any decorator that wraps a function.
7
-
Essentially, any higher-order function that could be written in a functional language with fixed arity should be variable arity in TypeScript in order to support the variety of Javascript uses.
8
-
With the ES2015 and ES2017 standards, this use will become even easier as programs start using spread arguments and rest parameters for both arrays and objects.
9
-
10
-
Currently, Typescript does not support the entire ES2015 spec for spread/rest in argument lists precisely because it has no way to type most spread arguments that do not match a rest argument.
11
-
And its support for tuples does not capture nearly all the tuple-like patterns in Javascript.
5
+
This proposal lets Typescript give types to higher-order functions that take a variable number of parameters.
6
+
Functions like this include `concat`, `apply`, `curry`, `compose` and almost any decorator that wraps a function.
7
+
In Javascript, these higher-order functions are expected to accept variadic functionsas arguments.
8
+
With the ES2015 and ES2017 standards, this use will become even more common as programmers start using spread arguments and rest parameters for both arrays and objects.
12
9
This proposal addresses these use cases with a single, very general typing strategy based on higher-order kinds.
13
10
14
-
## Preview example with `curry` and decorator
11
+
## Preview example with `curry`
15
12
16
-
`curry` for functions with two arguments is simple to write in Typescript:
13
+
`curry` for functions with two arguments is simple to write in Javascript and Typescript:
14
+
15
+
```js
16
+
functioncurry(f, a) {
17
+
returnb=>f(a, b);
18
+
}
19
+
```
20
+
21
+
and in Typescript with type annotations:
17
22
18
23
```ts
19
24
function curry<T, U, V>(f: (t:T, u:U) =>V, a:T): (b:U) =>V {
This function can be typed, but there is a dependency between `n` and the kind variables: `n===...T.length` must be true for the type to be correct.
92
-
I'm not sure whether this is code that should actually be supported.
93
-
Even if it is, a verbose type annotation is a worthwhile price to pay to handle homogenous lists in Typescript's fairly buttoned-down type system.
76
+
The examples section contains several examples of this.
94
77
95
78
The previous examples show tuples used for variadic kinds.
96
79
Note that, if JavaScript supported named arguments, then objects could also be expected to be spread into function calls, and Typescript would also need variadic object kinds.
0 commit comments