-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ReadonlyArray with rest parameters #15972
Comments
This limitation makes harder to use tslint-immutable package. |
Is the following the right place to start with implementing a fix? |
There are a few places where we make an assumption about rest params being the |
Union and intersections are another example (See #23440). function dist(x1: number, y1: number, x2: number, y2: number): number
function dist(point1: point, point2: point): number
function dist(...args: number[] | point[]): number {
// function body
} |
I think I'm hitting the same issue when I do something like this: declare interface R {
a: [string, number],
b: [boolean]
}
declare function on<T extends keyof R, K extends R[T]>(val: T, listener: (...args: K) => void): void;
// rest parameter must be array type ^ in ...args
on('b', (a) => {
}) as a workaround I've had to not use the splat args, and overload the method for each arity |
This is also an issue for conditional types inside generic class methods
|
Yeah I've just run into this issue...
The above code should work, but does not :( |
There is a temporary work around for some of the issues mentioned here, specifically the union types, until problem is resolved type F1 = (...args: number[] | string[]) => any; // Throws an error
type Fof<T extends any[], R> = (...args: T) => R;
type F2 = 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 |
type Fof<T extends any[], R> = (...args: T) => R; |
Ya
Ya I did thanks for pointing that out 👍 |
I have another (albeit slightly dysfunctional) use case impacted by this. Using Typescript 3.1.0-rc. class Pipeline<PS extends any[]> {
// Gives error:
//
// A rest element type must be an array type.
//
// NOTE: Technically incorrect because `PS` includes `PS[0]`
//
public constructor(fns: [HeadFn<PS[0]>, ...TailFns<PS>]) {
}
}
interface HeadFn<T> {
kind: 'head'
props: T
}
interface TailFn<T> {
kind: 'tail'
props: T
}
type TailFns<TS> = {
[K in keyof TS]: TailFn<TS[K]>
} Same error with a stronger restriction on the type TailFns<TS extends any[]> = {
[K in keyof TS]: TailFn<TS[K]>
} |
Of note, this is also an issue with tuple splat types in typescript > 3.0. i.e.
|
... same for TS 3.0 "Rest elements in tuple types": type MyTuple1 = [string, ...string[]];
// Error in TS 3.0/3.1:
type MyTuple2 = [string, ...ReadonlyArray<string>]; (I didn't find an own issue for that.) |
Subscribing for this |
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:
Which I just needed to change to:
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 :) |
@ericselkpc your first example now works in 3.3, my example above also now works. |
Looks like this issue was fixed in #29435 See #29435 (comment) |
I expect all three functions to be compiled successfully.
TypeScript doesn't allow using ReadonlyArray although it is the same as Array but readonly.
The text was updated successfully, but these errors were encountered: