-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Union Type made of two types of arrays fails to provide all array's methods #11205
Comments
It is probably same issue as #10620 |
Yes, essentially it is the same, the subtle difference is that when matching the signature, it is matching one that it cannot union the overload: interface Array<T> {
splice(start: number): T[];
splice(start: number, deleteCount: number, ...items: T[]): T[];
} It cannot union the type of the interface Array<T> {
splice(start: number): T[];
splice(start: number, deleteCount: number): T[];
splice(start: number, deleteCount: number, ...items: T[]): T[];
} But it is better to have |
Well, then this code does not compile: let x: (string[] | number[])[]
x.forEach(e => e.splice(1, 1)); // The same error Unfortunately I have both cases in my code. Whatever way I choose to describe my types I get this error :-(. |
This will work, but I suspect this is not what you want: let x: (string | number)[][]
x.forEach(e => e.splice(1, 1)); As I suspect you want an array of array of all numbers or an array of arrays of all strings. The other option would be to help TypeScript along: let x: (string[] | number[])[];
x.forEach((e: any[]) => e.splice(1, 1)); |
|
You can thank JavaScript for that... Coming from JavaScript to TypeScript (my path) is like finally getting medication to treat your insanity. Coming from C# must be like taking medical marijuana for the first time. Please don't let TypeScript be a gateway drug to the acid trip that is JavaScript though. |
I think we should rewrite the signatures to be
with the observation that generic types should have signatures split according to their input use of their type parameters |
TypeScript Version: 1.8.0 / TypeScript playground
Code
Expected behavior:
TS tutorial states:
Since both
x1: number[];
andx2: number[][]
havesplice
method with two params, I expect the union of these two types also to have this method.Actual behavior:
Compiler reports an error: Supplied parameters do not match any signature of call target. However, it allows using another overload of
splice
function:The text was updated successfully, but these errors were encountered: