0
0
Typescriptprogramming~20 mins

Why mapped types are needed in Typescript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mapped Types Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
ATypeError at runtime
B{"darkMode":true,"newUserProfile":false}
C{"darkMode":() => void,"newUserProfile":() => void}
DSyntaxError during compilation
Attempts:
2 left
💡 Hint
Mapped types transform property types, here to boolean values.
🧠 Conceptual
intermediate
1:30remaining
Purpose of mapped types in TypeScript
Why are mapped types needed in TypeScript?
ATo define runtime classes with inheritance.
BTo execute asynchronous code more efficiently.
CTo create new types by transforming properties of existing types systematically.
DTo handle errors during program execution.
Attempts:
2 left
💡 Hint
Think about how you can reuse and modify existing type structures.
🔧 Debug
advanced
2: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;
ANo error, code runs fine
BReferenceError: user is not defined
CTypeError: Cannot assign to 'age' because it is a read-only property
DSyntaxError: Missing type annotation after property in mapped type
Attempts:
2 left
💡 Hint
Check the syntax of the mapped type declaration.
Predict Output
advanced
2: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);
A{"id":1,"name":"Bob"}
BTypeError at runtime
CSyntaxError during compilation
D{"id":1,"name":"Bob","greet":undefined}
Attempts:
2 left
💡 Hint
The mapped type uses conditional key remapping to exclude function properties.
🧠 Conceptual
expert
2:00remaining
Why mapped types improve code maintainability
How do mapped types improve code maintainability in large TypeScript projects?
ABy allowing centralized and consistent transformations of types, reducing duplication and errors.
BBy automatically optimizing runtime performance of JavaScript code.
CBy replacing the need for interfaces and classes entirely.
DBy enabling dynamic type changes during program execution.
Attempts:
2 left
💡 Hint
Think about how changing one type definition affects many related types.