Improved keyof with intersection types
With TypeScript 2.8 keyof
applied to an intersection type is transformed to a union of keyof
applied to each intersection constituent.In other words, types of the form keyof (A & B)
are transformed to be keyof A | keyof B
.This change should address inconsistencies with inference from keyof
expressions.
Example
type A = { a: string };
type B = { b: string };
type T1 = keyof (A & B); // "a" | "b"
type T2<T> = keyof (T & B); // keyof T | "b"
type T3<U> = keyof (A & U); // "a" | keyof U
type T4<T, U> = keyof (T & U); // keyof T | keyof U
type T5 = T2<A>; // "a" | "b"
type T6 = T3<B>; // "a" | "b"
type T7 = T4<A, B>; // "a" | "b"