TypeScript expected `never` but got intersection

79 Views Asked by At

I was expecting a conflicting type intersection to produce a type of never. What am I missing?

type A = {value: string}
type B = {value: number}

type D = A & B

type E<T> = T extends never ? 'never' : 'something';
type F = E<D> // expected 'never', got 'something';
1

There are 1 best solutions below

0
T.J. Crowder On BEST ANSWER

It's only the conflicting property that has the never type, not the entire object type D (other properties in the type might be just fine, after all). So D["value"] is never, but not D:

type A = {value: string}
type B = {value: number}

type D = A & B

type E<T> = T extends never ? 'never' : 'something';
type F = E<D>;
//   ^?−− type F = "something"
type F2 = E<D["value"]>;
//   ^?−− type F2 = "never"

Playground link