Skip to content
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

Returned tuples don't match compatible types when using function union types #36053

Open
falsandtru opened this issue Jan 7, 2020 · 7 comments
Labels
Bug A bug in TypeScript
Milestone

Comments

@falsandtru
Copy link
Contributor

falsandtru commented Jan 7, 2020

The first error is especially odd.

TypeScript Version: 3.7.x-dev.20200104

Search Terms:

Code

type C1 = (a: 1) => [0];
type C2 = (a: 2) => [number];
type C3 = (a: 3) => undefined;

declare function f(a: C1 | C3): void;
f(() => [0]);
declare function g(a: C2 | C3): void;
g(() => [0]);

Expected behavior:
pass
Actual behavior:
Type 'number' is not assignable to type '0'.(2322)
Property '0' is missing in type 'number[]' but required in type '[number]'.(2741)
Playground Link: http://www.typescriptlang.org/play/index.html?ts=3.8.0-dev.20200104&ssl=1&ssc=1&pln=8&pc=14#code/C4TwDgpgBAwgjFAvFAFAQwFxTgSiQPigG0AGAXQG4AoUSWAJiVUynr0UKIDsBXAWwBGEAE6Ua4aDADMTdFintCPLgBMIAMwCWXCCupU1AYwA2aYdHXLDwTQHsuUdXNgIAPrAVYAbrc16qTiiKxOQ41Eam5o5WNvZQAObOMIzu0jjevv6JQQQhZGFAA

Related Issues:

@jcalz
Copy link
Contributor

jcalz commented Jan 7, 2020

Duplicate of #241

@falsandtru
Copy link
Contributor Author

I'm really not sure why you could think so. The compiler just chooses a type. Doesn't measure the variance.

@jcalz
Copy link
Contributor

jcalz commented Jan 7, 2020

The return type of () => [0] is widened to number[], despite contextual type information about the expected parameter of f() and g().

@falsandtru
Copy link
Contributor Author

Then can you explain why the following code won't be an error?

declare function f(a: C1): void;
f(() => [0]);

@jcalz
Copy link
Contributor

jcalz commented Jan 7, 2020

Magic? (No, I’m not sure.)

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jan 7, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jan 7, 2020
@RyanCavanaugh
Copy link
Member

Not sure which direction we should go on this one.

@falsandtru
Copy link
Contributor Author

@jcalz Your stupid comments are really useless and annoying. Don't touch my issues. I'm not posting issues to talk to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants