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
Func2 is a generic type with one type-argument. You provided no type-argument. When the type-argument has no default type it must be provided. Docs on generic types
The error message is quite clear here:
Generic type ' Func2' requires 1 type argument(s).
I guess you confused position of type parameters. If they are used in type alias declaration - you need to use it as Func2<Param>, most likely you wanted to make type parameter as part of function declaration type Func3 = <T extends any[]>(...args: T) => void;
This will be closer to Func1 but still produce different set of expected errors related to this FAQ.
Where justFunc can determinate type from first argument, but for types (for variables) you can't do that, because there's two different behaviours behind the single type.
(ALL) it feels somehow counter intuitive that 1st can work. I totally understand why any[] is not matching undefined in the second example, but the fact that it works in the 1st is at the same time very convenient and weird.
I would have expected that ...args: any[] expects the type of rest arguments to be array of any, but it seems that undefined also works implicitly.
It should, in my opinion, have been fn(...args?: any[]) to allow a call with no arguments.
(ALL) it feels somehow counter intuitive that 1st can work. I totally understand why any[] is not matching undefined in the second example, but the fact that it works in the 1st is at the same time very convenient and weird.
I would have expected that ...args: any[] expects the type of rest arguments to be array of any, but it seems that undefined also works implicitly.
It should, in my opinion, have been fn(...args?: any[]) to allow a call with no arguments.
FYI, you can assign default values for generic (type Func2<T extends any[] = []> = (...args: T) => void;) and you won't receive errors for const withoutParams2: Func2 = () => {}
Martin Johns (@MartinJohns) what i'm pointing out is the lack of consistency between Func1 and Func2 behaviors, which was explained by KitsoriK solution's to set default. but thanks for the constructive comment which just points out what i didn't understand, it really helped π
Activity
MartinJohns commentedon Sep 13, 2021
Func2is a generic type with one type-argument. You provided no type-argument. When the type-argument has no default type it must be provided. Docs on generic typesThe error message is quite clear here:
There's no defect here.
IllusionMH commentedon Sep 13, 2021
I guess you confused position of type parameters. If they are used in type alias declaration - you need to use it as
Func2<Param>, most likely you wanted to make type parameter as part of function declarationtype Func3 = <T extends any[]>(...args: T) => void;This will be closer to
Func1but still produce different set of expected errors related to this FAQ.Playground
rpidburachynskyi commentedon Sep 13, 2021
I think you can reach similar behaviour with function generics. Like that
Where
justFunccan determinate type from first argument, but for types (for variables) you can't do that, because there's two different behaviours behind the single type.y-nk commentedon Sep 14, 2021
thank you Andrii Dieiev (@IllusionMH) KitsoriK for the extended answer with example.
(ALL) it feels somehow counter intuitive that 1st can work. I totally understand why
any[]is not matchingundefinedin the second example, but the fact that it works in the 1st is at the same time very convenient and weird.I would have expected that
...args: any[]expects the type of rest arguments to be array of any, but it seems thatundefinedalso works implicitly.It should, in my opinion, have been
fn(...args?: any[])to allow a call with no arguments.MartinJohns commentedon Sep 14, 2021
An optional spread argument makes no sense and is invalid. But
fn(...args: any[])actually is callable without arguments.https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABMMAKAdJghgJwOYDOAXIlmAJ4DaAugJSIDeAvgLABQ7Kqt7QA
rpidburachynskyi commentedon Sep 14, 2021
FYI, you can assign default values for generic (
type Func2<T extends any[] = []> = (...args: T) => void;) and you won't receive errors forconst withoutParams2: Func2 = () => {}y-nk commentedon Sep 16, 2021
Martin Johns (@MartinJohns) what i'm pointing out is the lack of consistency between Func1 and Func2 behaviors, which was explained by KitsoriK solution's to set default. but thanks for the constructive comment which just points out what i didn't understand, it really helped π
KitsoriK that made sense a lot, thank you.