Challenge - 5 Problems
Mapped Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a conditional mapped type
What is the type of
Result after this code runs?Typescript
type Flags = {
a: string;
b: number;
c: boolean;
};
type ConditionalMapped<T> = {
[K in keyof T]: T[K] extends number ? string : boolean;
};
type Result = ConditionalMapped<Flags>;Attempts:
2 left
💡 Hint
Check which properties have type number and what the conditional changes them to.
✗ Incorrect
The mapped type checks each property type. If it extends number, it becomes string; otherwise boolean. So 'b' is number, becomes string; 'a' and 'c' become boolean.
❓ Predict Output
intermediate2:00remaining
Mapped type filtering keys by conditional
What is the type of
Filtered after this code runs?Typescript
type Data = {
id: number;
name: string;
active: boolean;
};
type FilterByType<T, U> = {
[K in keyof T as T[K] extends U ? K : never]: T[K];
};
type Filtered = FilterByType<Data, string>;Attempts:
2 left
💡 Hint
Look at the conditional key remapping and which keys match the type string.
✗ Incorrect
The mapped type keeps keys whose value type extends string. Only 'name' is string, so Filtered has only 'name'.
🔧 Debug
advanced2:00remaining
Why does this mapped type cause an error?
This code causes a TypeScript error. What is the cause?
Typescript
type Example<T> = {
[K in keyof T]: T[K] extends string ? K : never;
};
type Result = Example<{ a: string; b: number; }>;Attempts:
2 left
💡 Hint
Mapped types define property types, not keys as values.
✗ Incorrect
Mapped types define property types for each key. Returning keys (K) as values is invalid because keys are not types. The value must be a type, not a key name.
❓ Predict Output
advanced2:00remaining
Output type of nested conditional mapped type
What is the type of
NestedResult after this code runs?Typescript
type Nested<T> = {
[K in keyof T]: T[K] extends number ? string : T[K] extends boolean ? number : T[K];
};
type Input = { x: number; y: boolean; z: string };
type NestedResult = Nested<Input>;Attempts:
2 left
💡 Hint
Check each property type and apply the nested conditional rules carefully.
✗ Incorrect
For 'x' number → string; 'y' boolean → number; 'z' string → string (unchanged).
🧠 Conceptual
expert2:00remaining
How many keys remain after conditional key remapping?
Given this type, how many keys does
Final have?Typescript
type Original = {
a: string;
b: number;
c: boolean;
d: string | number;
};
type FilterKeys<T> = {
[K in keyof T as T[K] extends string ? K : never]: T[K];
};
type Final = FilterKeys<Original>;Attempts:
2 left
💡 Hint
Check which properties extend string exactly, not unions including string.
✗ Incorrect
Only 'a' is exactly string. 'd' is string | number, which does not extend string alone, so it is excluded. So Final has 1 key.