Mapped & Conditional TypesIntermediate
1Question 1 of 1
typescriptHow do Mapped Types and Conditional Types work in advanced TypeScript?
Explanation & Concept
• Mapped Types: Create new types by iterating over keys using `[K in keyof T]`. • Conditional Types: Express type relationships based on conditions using the ternary syntax `T extends U ? X : Y`.
Code Example
typescript
// Mapped type: Make all fields readonly and nullable
type NullableReadonly<T> = {
readonly [K in keyof T]: T[K] | null;
};
// Conditional type: Extract array element type
type ElementType<T> = T extends (infer U)[] ? U : T;
type NumberList = number[];
type SingleNum = ElementType<NumberList>; // resolved to: numberHow It Works & Key Takeaways
Conditional types with `infer` allow pulling internal types out of complex wrapper structures like Promises, functions, and arrays.
Mock Test · 1 Questions · Optional
Ready for Decorators?
Take a quick test to evaluate yourself, or move straight to the next module.
Intermediate1 questions