Skip to content

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

Closed

Description

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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions