Skip to content

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

Closed
NikitaEgorov opened this issue Aug 7, 2015 · 9 comments
Closed

Union types and array.concat problem #4216

NikitaEgorov opened this issue Aug 7, 2015 · 9 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@NikitaEgorov
Copy link

var a = (): number | Array<number> => {
    return Math.random() > 0.5? [2,3] : 3;
};

var b = [1];

b.concat(a());

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

@zpdDG4gta8XKpMCd
Copy link

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?

@darrylring
Copy link

Which is the slightly unhelpful way of saying that union types aren't currently compatible with overloaded functions (see #1805). Neither overload of concat accepts number | number[].

From lib.d.ts:

concat<U extends T[]>(...items: U[]): T[];
concat(...items: T[]): T[];

@zpdDG4gta8XKpMCd
Copy link

Pardon me, didn't know concat is designed so loose.

@darrylring
Copy link

I'd say it's the opposite of loose. In JavaScript you could do:

b.concat(2, [3,4], 5, [6]);

The way concat is defined in lib.d.ts forbids this. You can give it one or more T or T[], but not both.

@zpdDG4gta8XKpMCd
Copy link

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

@darrylring
Copy link

Ah, gotcha. I also think lib.d.ts is probably correct.

@weswigham
Copy link
Member

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.

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Aug 10, 2015
@mhegazy mhegazy closed this as completed Aug 10, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Aug 10, 2015

Looks like the original issue has been answered. please reactivate if you are running into other issues.

@pmowrer
Copy link

pmowrer commented Apr 27, 2016

Here's an active thread on this problem: #6594

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

6 participants