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
Expected behavior: Successful compilation. Array subclasses are not allowed as rest arguments for now, but when it is about generics I expect behavior similar to what is done in #24897
Actual behavior: Failed with TS2574: A rest element type must be an array type.
Interstingly, adding angled brackets works (but requires an array of arrays following the number):
interfaceFoo<ArgsTextendsany[]>{arr: ArgsT;}interfaceBar<SubArgsTextendsany[]>extendsFoo<[number, ...SubArgsT[]]>{}varf: Foo<number[]>={arr: [1,2,3],};varb1: Bar<string[]>={arr: [1,[""],["1","2"]],// not what OP meant, but it works}varb2: Bar<string[]>={arr: [1,"","1","2"],// error: [number, string, string] cannot be assigned to [number, ...string[][]]}
And this correctly captures the type OP wants to create
Similarly, it seems that following the last example of AlCalzone that rest element types should be able to be inferred without having to use a functions parameter list like so:
typedropFirst<Textendsany[]>=Textends[any, ...(infer U)] ? U : T;// Instead oftypedropFirst<Textendsany[]>=((...args: T)=>any)extends(arg: any, ...rest: infer U)=>any ? U : T;
But the same error occurs: A rest element type must be an array type.
Reacted by csha, Eric Simonton, Ran Lottem, Augusto Moura, Micah Zoltu, James Conkling, Salathiel Genese, ExE Boss, Stepan Zubashev, Amine Ben hammou and 3 more
A minimal example showing the non-symmetricity of function arguments vs. tuple types.
declarefunctionfoo<T0,Textendsany[]>(a: T0, ...b: T);// workstypefoo<T0,Textendsany[]>=[T0, ...T];// rest element type must be an array type
I think this should be supported and it seems as an oversight to me that it isn't in 3.1 already.
Is there a specific reason for it to be disallowed, or is it just that it wasn't top priority?
Also, it took me quite a long time to find this issue, so I'm adding a few keywords.
Keywords: spread a generic tuple in a tuple type, generic tuple rest parameter in a tuple type, spreading array type in type declaration, T extends any[] does not work in tuple literal
Reacted by Vladimir Rindevich, Jake Teton-Landis, Dylan C. Kendal, Colin Kahn, Ran Lottem, Calvin, Linus Unnebäck, Paul Maréchal, ExE Boss, Amin Pakseresht and 8 more
Activity
AlCalzone commentedon Aug 1, 2018
Interstingly, adding angled brackets works (but requires an array of arrays following the number):
And this correctly captures the type OP wants to create
but an interface cannot extend that.
McPrescott commentedon Sep 10, 2018
Similarly, it seems that following the last example of AlCalzone that rest element types should be able to be inferred without having to use a functions parameter list like so:
But the same error occurs: A rest element type must be an array type.
cshaa commentedon Oct 16, 2018
A minimal example showing the non-symmetricity of function arguments vs. tuple types.
I think this should be supported and it seems as an oversight to me that it isn't in 3.1 already.
Is there a specific reason for it to be disallowed, or is it just that it wasn't top priority?
Also, it took me quite a long time to find this issue, so I'm adding a few keywords.
Keywords: spread a generic tuple in a tuple type, generic tuple rest parameter in a tuple type, spreading array type in type declaration,
T extends any[]does not work in tuple literalpaul-marechal commentedon Aug 25, 2019
I would like to extend this issue for code looking like the following:
Hopefully this all boils down to the same actual bug.
ahejlsberg commentedon Jun 17, 2020
This is now implemented in #39094.