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
There are a few places where we make an assumption about rest params being the globalArrayType, we will need to look at them all and fix them. one place is for example any use of isArrayType with rest params.
I think I'm hitting the same issue when I do something like this:
declareinterfaceR{a: [string,number],b: [boolean]}declarefunctionon<TextendskeyofR,KextendsR[T]>(val: T,listener: (...args: K)=>void): void;// rest parameter must be array type ^ in ...argson('b',(a)=>{})
as a workaround I've had to not use the splat args, and overload the method for each arity
Reacted by Michael Sanford, Anton Zinovyev and Keegan Farley
There is a temporary work around for some of the issues mentioned here, specifically the union types, until problem is resolved
typeF1=(...args: number[]|string[])=>any;// Throws an errortypeFof<Textendsany[],R>=(...args: T)=>R;typeF2=Fof<number[]|string[],any>;// Compiles without error
It's not clear to me why this fix works. As you can see in the screenshot below, the type of F2 is (...args: number[] | string[]) => any even though that type is apparently an error.
I have another (albeit slightly dysfunctional) use case impacted by this. Using Typescript 3.1.0-rc.
classPipeline<PSextendsany[]>{// Gives error://// A rest element type must be an array type.//// NOTE: Technically incorrect because `PS` includes `PS[0]`//publicconstructor(fns: [HeadFn<PS[0]>, ...TailFns<PS>]){}}interfaceHeadFn<T>{kind: 'head'props: T}interfaceTailFn<T>{kind: 'tail'props: T}typeTailFns<TS>={[KinkeyofTS]: TailFn<TS[K]>}
Same error with a stronger restriction on the TailFns type parameter:
Of note, this is also an issue with tuple splat types in typescript > 3.0.
i.e.
// works
function tuple<T extends any[]>(...value: T) {
return value
}
// compile error
function tuple<T extends ReadonlyArray<any>>(...value: T) {
return value
}
For those that land here like I did and think they need to use the Fof<> workaround above, in my case it was much easier. I had this:
add: (...items: T[] | Enum<T>[]) => void;
Which I just needed to change to:
add: (...items: (T | Enum<T>)[]) => void;
Might be obvious to all but noobs, but since this was the first hit on Google when I searched on the error message, safe to say I'm not the only noob reading this.
ericselkpc It may not matter terribly much, but I'd just like to point out that this solution has one technical problem - it allows mixed arrays... ie. an array that contains items of both type T and type Enum... This may cause problems if you're not careful :)
Activity
NN--- commentedon Sep 19, 2017
This limitation makes harder to use tslint-immutable package.
https://www.npmjs.com/package/tslint-immutable
adieuadieu commentedon Feb 6, 2018
Is the following the right place to start with implementing a fix?
https://github.com/Microsoft/TypeScript/blob/14bd0a2d5678d89c474443f02841654c7d014b40/src/compiler/utilities.ts#L930-L940
mhegazy commentedon Feb 6, 2018
There are a few places where we make an assumption about rest params being the
globalArrayType, we will need to look at them all and fix them. one place is for example any use ofisArrayTypewith rest params.mhegazy commentedon Apr 16, 2018
Union and intersections are another example (See #23440).
alexander-alvarez commentedon May 21, 2018
I think I'm hitting the same issue when I do something like this:
as a workaround I've had to not use the splat args, and overload the method for each arity
robbiespeed commentedon Jun 12, 2018
This is also an issue for conditional types inside generic class methods
sam-s4s commentedon Sep 6, 2018
Yeah I've just run into this issue...
The above code should work, but does not :(
McPrescott commentedon Sep 11, 2018
There is a temporary work around for some of the issues mentioned here, specifically the union types, until problem is resolved
It's not clear to me why this fix works. As you can see in the screenshot below, the type of
F2is(...args: number[] | string[]) => anyeven though that type is apparently an error.NN--- commentedon Sep 11, 2018
McPrescott commentedon Sep 11, 2018
Ya
Ya I did thanks for pointing that out 👍
sullvn commentedon Sep 14, 2018
I have another (albeit slightly dysfunctional) use case impacted by this. Using Typescript 3.1.0-rc.
Same error with a stronger restriction on the
TailFnstype parameter:forivall commentedon Nov 1, 2018
Of note, this is also an issue with tuple splat types in typescript > 3.0.
i.e.
ulrichb commentedon Nov 13, 2018
... same for TS 3.0 "Rest elements in tuple types":
(I didn't find an own issue for that.)
megabayt commentedon Feb 7, 2019
Subscribing for this
ericselkpc commentedon Feb 11, 2019
For those that land here like I did and think they need to use the Fof<> workaround above, in my case it was much easier. I had this:
add: (...items: T[] | Enum<T>[]) => void;Which I just needed to change to:
add: (...items: (T | Enum<T>)[]) => void;Might be obvious to all but noobs, but since this was the first hit on Google when I searched on the error message, safe to say I'm not the only noob reading this.
sam-s4s commentedon Feb 11, 2019
ericselkpc It may not matter terribly much, but I'd just like to point out that this solution has one technical problem - it allows mixed arrays... ie. an array that contains items of both type T and type Enum... This may cause problems if you're not careful :)
robbiespeed commentedon Feb 13, 2019
ericselkpc your first example now works in 3.3, my example above also now works.
ikokostya commentedon Feb 14, 2019
Looks like this issue was fixed in #29435
See #29435 (comment)
/cc Anders Hejlsberg (@ahejlsberg) Daniel Rosenwasser (@DanielRosenwasser)