Advanced Types & Utilities Flashcards
Mapped Types Conditional Types Utility Types (Partial, Required, Readonly, Pick, Omit, Record) keyof, typeof, infer, in (48 cards)
What is a mapped type in TypeScript?
A mapped type creates new types by transforming properties of an existing type using constructs like in
and keyof
.
What is a conditional type in TypeScript?
A conditional type uses a ternary expression to choose between types based on a condition, like T extends U ? X : Y
.
What does the utility type Partial<T> do?</T>
Partial<T>
makes all properties of type T optional.
What does the utility type Required<T> do?</T>
Required<T>
makes all properties of type T required.
What does the utility type Readonly<T> do?</T>
Readonly<T>
makes all properties of type T read-only.
What does the utility type Pick<T, K> do?
Pick<T, K>
creates a new type by selecting a subset K of properties from type T.
What does the utility type Omit<T, K> do?
Omit<T, K>
creates a new type by excluding properties K from type T.
What does the utility type Record<K, T> do?
Record<K, T>
creates a type with keys K and values of type T.
What does ‘keyof’ return in TypeScript?
keyof
returns a union of property names (as strings) of a given type.
What does ‘typeof’ return in TypeScript?
typeof
returns the type of a variable, used to infer its type in type declarations.
What does ‘infer’ do in TypeScript?
infer
is used within conditional types to infer a type inside a condition and reuse it.
What does ‘in’ do in TypeScript types?
in
is used in mapped types to iterate over keys in a union or another type.
What is an advantage of using mapped types?
They enable creation of flexible, reusable, and type-safe modifications of existing types.
What is a disadvantage of conditional types?
They can become complex and hard to read, especially when deeply nested or combined with infer.
What is a use case for Partial<T>?</T>
When updating a subset of properties in a form or data structure.
What is a use case for Readonly<T>?</T>
When you want to protect data from being modified after creation.
What is a use case for Pick<T, K>?
When selecting specific fields from a larger interface for a component or function.
What is a use case for Omit<T, K>?
When removing sensitive or irrelevant fields before exposing an object.
What is a use case for Record<K, T>?
When you want to create an object type with known keys and uniform value types.
What’s a best practice for using Partial<T>?</T>
Only use Partial<T>
when fields are truly optional—otherwise use default values.
What’s a best practice for using Omit<T, K>?
Prefer it when you need to hide or exclude certain properties from the exposed types.
What is a potential gotcha with infer?
infer
only works inside conditional types and can lead to obscure code if overused.
What’s a real-world tradeoff with mapped types?
They reduce duplication but can introduce complexity when debugging nested generics.
What’s a debugging tip for conditional types?
Break them into smaller, testable components and check inferred types using IDEs or type playgrounds.