interface Person {
name: string;
age: number;
location: string;
}
type K1 = keyof Person; // "name" | "age" | "location"
type K2 = keyof Person[]; // "length" | "push" | "pop" | "concat" | ...
type K3 = keyof { [x: string]: Person }; // string
enum ColorsEnum {
white = '#ffffff',
black = '#000000',
}
type Color = keyof typeof ColorsEnum; // 'white' | 'black'
let colorLiteral: Color
colorLiteral = "white" // OK
colorLiteral = "black" // OK
colorLiteral = "red" // Error...
type staffKeys = 'name' | 'salary';
function getProperty<T, K extends staffKeys>(obj: T, key: K): T[K] {
return obj[key];
}
type Arrayish = { [n: number]: unknown };
type A = keyof Arrayish;
type A = number
type Mapish = { [k: string]: boolean };
type M = keyof Mapish;
type M = string | number
Try