/** * @format * @flow */ type A = {a: number}; type B = {b: string}; declare const k: keyof (A & B); k as 'a' | 'b'; // ok k as 'a'; // error: 'b' is a key too k as 'b'; // error: 'a' is a key too type Without = T extends U ? empty : T; 'b' as Without; // ok 'a' as Without; // error declare const p: {[K in Without]: (A & B)[K]}; p.b as string; // ok p.a; // error // A member that is not an object has no keys. declare const lit: keyof ({c: number} & 'lit'); lit as 'c'; // ok function generic(key: keyof (T & {c: number})): 'c' { return key; // ok } function bounded(key: keyof (T & {c: number})): 'c' { return key; // error: 'd' is a key too } declare const none: keyof (number & 'lit'); none as empty; // ok declare const arr: keyof (Array & {[key: string]: number}); arr as string; // ok declare const opt: keyof ((void | {d: number}) & {c: number}); opt as 'c'; // error: 'd' is a key too // An element of an intersection is read key by key. declare const ab: A & B; ab[k] as number | string; // ok ab[k] as number; // error: `b` is a string function read(key: K): number | string { return ab[key]; // ok } // So are its values. declare const values: Values; values as number | string; // ok values as number; // error: `b` is a string