Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface Colours {
- red: string;
- green: string;
- blue: string[];
- }
- let colours: Colours;
- export type PropertyNamesOfType<T, TT> = { [K in keyof T]: T[K] extends TT ? K : never }[keyof T] & string;
- function getColourStr(colour: PropertyNamesOfType<Colours, string>): string {
- return colours[colour] as string;
- }
- function getColourArr(colour: PropertyNamesOfType<Colours, string[]>): string[] {
- return colours[colour] as string[];
- }
- // Success
- getColourStr('red');
- getColourArr('blue');
- // Error
- getColourStr('blue');
- getColourArr('red');
Add Comment
Please, Sign In to add comment