0
0
Typescriptprogramming~20 mins

Mapped type with conditional types in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mapped Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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>;
A{ a: string; b: string; c: boolean; }
B{ a: boolean; b: boolean; c: boolean; }
C{ a: boolean; b: number; c: boolean; }
D{ a: boolean; b: string; c: boolean; }
Attempts:
2 left
💡 Hint
Check which properties have type number and what the conditional changes them to.
Predict Output
intermediate
2: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>;
A{ name: string; }
B{ id: number; }
C{ id: number; name: string; active: boolean; }
D{ active: boolean; }
Attempts:
2 left
💡 Hint
Look at the conditional key remapping and which keys match the type string.
🔧 Debug
advanced
2: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; }>;
AThe type parameter T is missing a constraint causing error.
BMapped type values must be types, but here keys are used as values causing error.
CKeys cannot be used in mapped types, only values are allowed.
DThe conditional expression is invalid syntax inside mapped types.
Attempts:
2 left
💡 Hint
Mapped types define property types, not keys as values.
Predict Output
advanced
2: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>;
A{ x: string; y: boolean; z: string; }
B{ x: number; y: number; z: string; }
C{ x: string; y: number; z: string; }
D{ x: string; y: string; z: string; }
Attempts:
2 left
💡 Hint
Check each property type and apply the nested conditional rules carefully.
🧠 Conceptual
expert
2: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>;
A1
B2
C3
D4
Attempts:
2 left
💡 Hint
Check which properties extend string exactly, not unions including string.