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

The behavior is different for two code that looks the same #30988

Closed
kgtkr opened this issue Apr 17, 2019 · 3 comments
Closed

The behavior is different for two code that looks the same #30988

kgtkr opened this issue Apr 17, 2019 · 3 comments
Labels
Unactionable There isn't something we can do with this issue

Comments

@kgtkr
Copy link

kgtkr commented Apr 17, 2019

TypeScript Version: 3.4.2

Search Terms:
conditional type

Code

OK

type F<X> = <T>() => T extends X ? 1 : 2;

declare const x: F<any>;
const y: F<number> = x;

Error

declare const x: <T>() => T extends any ? 1 : 2;
const y: <T>() => T extends number ? 1 : 2 = x;

Expected behavior:
The two codes do the same thing

Actual behavior:
Different behavior

Playground Link:

Related Issues:

@jack-williams
Copy link
Collaborator

I think the effect of the type alias F is causing reduction in the body of the alias, in particular, I think it's simplifying the return type to the constraint of the conditional 1 | 2. Once this happens, the type parameter X no longer appears in the type and the two signatures are related.

type F<X> = <T>() => T extends X ? 1 : 2;
declare const x: F<number>;
const y: F<string> = x; // no error

type F2<X> = <T>() => T extends X ? 1 : 2;
const y2: F2<string> = x; // error

Haven't got as far as tracking down where exactly this behaviour comes from, but I'm guessing there is some instantiation business going on.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Apr 17, 2019
@kgtkr
Copy link
Author

kgtkr commented Apr 18, 2019

Related code

image

@RyanCavanaugh RyanCavanaugh added Unactionable There isn't something we can do with this issue and removed Needs Investigation This issue needs a team member to investigate its status. labels Aug 22, 2019
@RyanCavanaugh
Copy link
Member

These samples are quite different and it's not apparent why they must behave identically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Unactionable There isn't something we can do with this issue
Projects
None yet
Development

No branches or pull requests

3 participants