Skip to content

Uncomputed generic type does not follow structural typing #52096

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
quangloc99 opened this issue Jan 4, 2023 · 1 comment · Fixed by #52106
Open

Uncomputed generic type does not follow structural typing #52096

quangloc99 opened this issue Jan 4, 2023 · 1 comment · Fixed by #52106
Assignees
Labels
Bug A bug in TypeScript Rescheduled This issue was previously scheduled to an earlier milestone

Comments

@quangloc99
Copy link

Bug Report

🔎 Search Terms

generic
unassignable type
uncomputed type

🕗 Version & Regression Information

⏯ Playground Link

Playground link with relevant code

💻 Code

class A {
    x: string = 'A';
    y: number = 0;
}

class B {
    x: string = 'B';
    z: boolean = true;
}

type T<X extends { x: any }> = Pick<X, 'x'>;

type C = T<A>;
type D = T<B>;

type C_extends_D = C extends D ? true : false;                                  // true
type PickA_extends_PickB = Pick<A, 'x'> extends Pick<B, 'x'> ? true : false;    // true
type TA_extends_TB = T<A> extends T<B> ? true : false;                          // false (?)

declare let a: T<A>;
declare let b: T<B>;
declare let c: C;
declare let d: D;

b = a;      // error here (?)
c = d;

🙁 Actual behavior

  • Type TA_extends_TB is false.
  • a is not assignable to b.

🙂 Expected behavior

  • All types TA_extends_TB should be true, the same way as C_extends_D and PickA_extends_PickB.
  • a should be assignable to b and vice-versa, the same way as c and d.
@fatcerberus
Copy link

fatcerberus commented Jan 4, 2023

Looks like a variance measurement issue. C and D are compared structurally while T<A> and T<B> get compared by variance (i.e. by directly comparing their type arguments), which always fails because neither A nor B is assignable to the other.

This is the vital clue:

Property 'z' is missing in type 'A' but required in type 'B'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Rescheduled This issue was previously scheduled to an earlier milestone
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants
@DanielRosenwasser @weswigham @fatcerberus @RyanCavanaugh @quangloc99 and others