All quizzesHard
TypeScript Advanced — Series 2
Preview — 3 of 10 questions
Which keys are included in DataKeys?
javascript
type NonFunctionKeys<T> = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T];
interface Config { host: string; port: number; connect(): void; }
type DataKeys = NonFunctionKeys<Config>;ADataKeys is keyof Config, unchanged
BDataKeys is "connect" only
CDataKeys is "host" | "port".
DCompile-time error — a mapped type can't conditionally resolve to never
Which properties and methods exist on PersonGetters?
javascript
interface Person { name: string; age: number; }
type Getters<T> = { [K in keyof T as `get${Capitalize<string & K>}`]: () => T[K] };
type PersonGetters = Getters<Person>;
declare const g: PersonGetters;
g.getName();
g.name;Ag.getName() returns string; g.name is a type error after key remapping.
BPersonGetters ends up with both the original keys (name, age) and the renamed getter keys (getName, getAge)
CCompile-time error — Capitalize can't be used inside a key-remapping template literal
DBoth g.getName and g.name are valid, equivalent ways to access the same underlying value
What types do A and B resolve to?
javascript
type Last<T extends unknown[]> = T extends [...infer _, infer L] ? L : never;
type A = Last<[1, 2, 3]>;
type B = Last<[]>;AA is 3; B is never because the empty tuple has no last element.
BA is the whole tuple [1, 2, 3], and B is never
CA is 3, and B is undefined
DCompile-time error — infer can only appear once per conditional type
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.