A function's formal parameters are defined as parameter_name
followed by an optional type annotation. That is name
or name: type annotation
If no type annotation is specified and none can be inferred, the parameter will have type any
.
In the case of a rest parameter, written ...parameter
, if no type annotation is specified and none can be inferred, ...parameter
will have type any[]
.
Therefore, in the
type T_b = (...T_b_arguments) => boolean
T_b_arguments
does not refer to a type but is the name of a rest parameter of type any[]
To correct this, you must name the parameter and apply the type annotation, T_b_arguments
type T_b = (...args: T_b_arguments) => boolean
Note that the ...
syntax is not applied to the parameter type but to the parameter name.
Here is the fully working code
type T_a = (a1: string, a2: number) => boolean
const a: T_a = (a1, a2) => a1.length >= a2
type T_b_arguments = [number, string]
type T_b = (...args: T_b_arguments) => boolean
const b: T_b = (b1, b2) => b1 < b2.length
b(1, 'mystring') // true
Playground Link
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…