-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Union types and array.concat problem #4216
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
Comments
in the left hand you have apples, in the right hand you have apples and oranges, now you are confused: when you put your hands together those oranges that you hold in the right hand don't turn into apples, why is that? |
Which is the slightly unhelpful way of saying that union types aren't currently compatible with overloaded functions (see #1805). Neither overload of From lib.d.ts: concat<U extends T[]>(...items: U[]): T[];
concat(...items: T[]): T[]; |
Pardon me, didn't know |
I'd say it's the opposite of loose. In JavaScript you could do: b.concat(2, [3,4], 5, [6]); The way |
that's what I meant by saying it's designed loose in JavaScript, didn't expect it to be this way, the original observation is valid, technically libs.d.ts should be revised, although personally I'd keep it the way it is, but it's a question of what you believe in |
Ah, gotcha. I also think |
You can retype the original code like so: declare type RandomResult = number | Array<number>;
var a = (): RandomResult => {
return Math.random() > 0.5? [2,3] : 3;
};
var b: RandomResult[] = [1];
b.concat(a()); And it passes the typechecker. Possibly not necessarily for the expected reasons. |
Looks like the original issue has been answered. please reactivate if you are running into other issues. |
Here's an active thread on this problem: #6594 |
http://www.typescriptlang.org/Playground#src=var%20a%20%3D%20()%3A%20number%20%7C%20Array%3Cnumber%3E%20%3D%3E%20%7B%0A%09return%20Math.random()%20%3E%200.5%3F%20%5B2%2C3%5D%20%3A%203%3B%0A%7D%3B%0A%0Avar%20b%20%3D%20%5B1%5D%3B%0A%0Ab.concat(a())%3B
The text was updated successfully, but these errors were encountered: