Advanced Generics

Preview — 3 of 10 questions

What is the result of T = string | number | boolean?

javascript
type IsString<T> = T extends string ? "yes" : "no";

type R1 = IsString<string>;            // ?
type R2 = IsString<number>;            // ?
type R3 = IsString<string | number>;   // ?
AR1 = "yes", R2 = "no", R3 = "no" — the union as a whole is not a string
BAll three are "yes" | "no"
CR1 = "yes", R2 = "no", R3 = "yes" — union is compatible with string
DR1 = "yes", R2 = "no", R3 = "yes" | "no" — distributive evaluation per union member

What is the type of result?

javascript
type Concat<T extends readonly unknown[], U extends readonly unknown[]> = [...T, ...U];

type Result = Concat<[string, number], [boolean, Date]>;
A(string | number | boolean | Date)[] — a regular array
B[string, number, boolean, Date]
Cstring | number | boolean | Date — a union
D[[string, number], [boolean, Date]] — a nested tuple

What are the results?

javascript
type Animal = 'cat' | 'dog' | 'fish' | 'bird';

type Pets  = Extract<Animal, 'cat' | 'dog'>;
type Wild  = Exclude<Animal, 'cat' | 'dog'>;
APets = Animal, Wild = never
BPets = 'fish' | 'bird', Wild = 'cat' | 'dog'
CPets = 'cat' | 'dog', Wild = 'fish' | 'bird'
DPets = never, Wild = Animal

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.