Skip to content

Conditional type *without union* behaves differently when inlined #36135

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
laughinghan opened this issue Jan 10, 2020 · 0 comments · Fixed by #37348
Closed

Conditional type *without union* behaves differently when inlined #36135

laughinghan opened this issue Jan 10, 2020 · 0 comments · Fixed by #37348
Assignees
Labels
Bug A bug in TypeScript

Comments

@laughinghan
Copy link

TypeScript Version: 3.7.2

Search Terms: inline, generic type, conditional type, Extract

Code

type MyExtract<T, U> = T extends U ? T : never

function foo<T>(a: T) {
  const b: Extract<any[], T> = 0 as any;
  a = b; // ok

  const c: (any[] extends T ? any[] : never) = 0 as any;
  a = c; // FAILS
//^ Type 'any[] extends T ? any[] : never' is not assignable to type 'T'.
//    'any[] extends T ? any[] : never' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
//      Type 'any[]' is not assignable to type 'T'.
//        'any[]' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.(2322)

  const d: MyExtract<any[], T> = 0 as any;
  a = d; // ok

  type CustomType = any[] extends T ? any[] : never;
  const e: CustomType = 0 as any;
  a = e; // FAILS
//^ Type 'CustomType' is not assignable to type 'T'.
//    'CustomType' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
//      Type 'any[]' is not assignable to type 'T'.
//        'any[]' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.(2322)
}

Expected behavior: All assignments typecheck

Actual behavior: Assignments with inlined conditional type all report type error

Playground Link

Related Issues:
These are similar because they involve a generic type behaving differently when inlined, but they're because of the distributive property of conditional types and homomorphic mapped types, which doesn't apply to this issue:

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Jan 14, 2020
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.9.0 milestone Jan 14, 2020
@weswigham weswigham added Bug A bug in TypeScript and removed Needs Investigation This issue needs a team member to investigate its status. labels Mar 11, 2020
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

Successfully merging a pull request may close this issue.

3 participants