Skip to content

Conditional types behavior is different when referencing same type with different name #44945

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

Open
k8w opened this issue Jul 8, 2021 · 1 comment
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Rescheduled This issue was previously scheduled to an earlier milestone

Comments

@k8w
Copy link

k8w commented Jul 8, 2021

Bug Report

πŸ”Ž Search Terms

conditional types different name

πŸ•— Version & Regression Information

v4.4.0-beta

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about conditional types

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Left<Z> = <T>() => (T extends Z ? 1 : 2);
type Right<Z> = <T>() => (T extends Z ? 1 : 2);

type Equal1<X, Y> = Left<X> extends Right<Y> ? true : false;
type Equal2<X, Y> = Left<X> extends Left<Y> ? true : false;

// false
type X1 = Equal1<{a:string}, {readonly a: string}>
// true
type X2 = Equal2<{a:string}, {readonly a: string}>

πŸ™ Actual behavior

X1 and X2 is different.

πŸ™‚ Expected behavior

X1 and X2 is the same.

Actually, Left and Right are the same, Equal1 and Equal2 are the same.
But the result of Equal1 and Equal2 with the same parameters are different.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jul 8, 2021
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.5.0 milestone Jul 8, 2021
@weswigham
Copy link
Member

See PR #43887 - this is essentially cause by how we compare Left via variances in Equal2 (and get an incorrect result because of the erased type parameter and variance not capturing that conditional extends clauses actually require identity and not a covarying or contravariant relationship), while we compare Left and Right structurally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Rescheduled This issue was previously scheduled to an earlier milestone
Projects
None yet
3 participants