Challenge - 5 Problems
Mapped Types Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a mapped type usage
What is the output of the following TypeScript code when compiled and run with Node.js (ignoring type errors)?
Typescript
type OptionsFlags<Type> = {
[Property in keyof Type]: boolean;
};
interface Features {
darkMode: () => void;
newUserProfile: () => void;
}
const featureOptions: OptionsFlags<Features> = {
darkMode: true,
newUserProfile: false
};
console.log(featureOptions);Attempts:
2 left
💡 Hint
Mapped types transform property types, here to boolean values.
✗ Incorrect
The mapped type OptionsFlags converts each property type of Features to boolean. So the object featureOptions has boolean values for each key.
🧠 Conceptual
intermediate1:30remaining
Purpose of mapped types in TypeScript
Why are mapped types needed in TypeScript?
Attempts:
2 left
💡 Hint
Think about how you can reuse and modify existing type structures.
✗ Incorrect
Mapped types allow you to create new types by applying a transformation to each property of an existing type, enabling flexible and reusable type definitions.
🔧 Debug
advanced2:30remaining
Identify the error in mapped type usage
What error does this TypeScript code produce?
Typescript
type Readonly<T> = {
readonly [P in keyof T];
};
interface User {
name: string;
age: number;
}
const user: Readonly<User> = {
name: "Alice",
age: 30
};
user.age = 31;Attempts:
2 left
💡 Hint
Check the syntax of the mapped type declaration.
✗ Incorrect
The mapped type syntax is missing the type annotation after the property name. It should be [P in keyof T]: T[P];
❓ Predict Output
advanced2:00remaining
Output of conditional mapped type
What is the output of this TypeScript code when compiled and run with Node.js (ignoring type errors)?
Typescript
type RemoveFunctions<T> = {
[P in keyof T as T[P] extends Function ? never : P]: T[P];
};
interface Mixed {
id: number;
name: string;
greet: () => void;
}
const obj: RemoveFunctions<Mixed> = {
id: 1,
name: "Bob"
};
console.log(obj);Attempts:
2 left
💡 Hint
The mapped type uses conditional key remapping to exclude function properties.
✗ Incorrect
Using key remapping with conditional types, function properties are excluded from the resulting type. Thus, obj only includes non-function properties id and name.
🧠 Conceptual
expert2:00remaining
Why mapped types improve code maintainability
How do mapped types improve code maintainability in large TypeScript projects?
Attempts:
2 left
💡 Hint
Think about how changing one type definition affects many related types.
✗ Incorrect
Mapped types let you define transformations once and apply them everywhere, so changes propagate consistently and reduce bugs.