Advanced Types & Utilities Flashcards

Mapped Types Conditional Types Utility Types (Partial, Required, Readonly, Pick, Omit, Record) keyof, typeof, infer, in (48 cards)

1
Q

What is a mapped type in TypeScript?

A

A mapped type creates new types by transforming properties of an existing type using constructs like in and keyof.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a conditional type in TypeScript?

A

A conditional type uses a ternary expression to choose between types based on a condition, like T extends U ? X : Y.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the utility type Partial<T> do?</T>

A

Partial<T> makes all properties of type T optional.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the utility type Required<T> do?</T>

A

Required<T> makes all properties of type T required.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the utility type Readonly<T> do?</T>

A

Readonly<T> makes all properties of type T read-only.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the utility type Pick<T, K> do?

A

Pick<T, K> creates a new type by selecting a subset K of properties from type T.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the utility type Omit<T, K> do?

A

Omit<T, K> creates a new type by excluding properties K from type T.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does the utility type Record<K, T> do?

A

Record<K, T> creates a type with keys K and values of type T.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does ‘keyof’ return in TypeScript?

A

keyof returns a union of property names (as strings) of a given type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does ‘typeof’ return in TypeScript?

A

typeof returns the type of a variable, used to infer its type in type declarations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does ‘infer’ do in TypeScript?

A

infer is used within conditional types to infer a type inside a condition and reuse it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does ‘in’ do in TypeScript types?

A

in is used in mapped types to iterate over keys in a union or another type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is an advantage of using mapped types?

A

They enable creation of flexible, reusable, and type-safe modifications of existing types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a disadvantage of conditional types?

A

They can become complex and hard to read, especially when deeply nested or combined with infer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a use case for Partial<T>?</T>

A

When updating a subset of properties in a form or data structure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a use case for Readonly<T>?</T>

A

When you want to protect data from being modified after creation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is a use case for Pick<T, K>?

A

When selecting specific fields from a larger interface for a component or function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is a use case for Omit<T, K>?

A

When removing sensitive or irrelevant fields before exposing an object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is a use case for Record<K, T>?

A

When you want to create an object type with known keys and uniform value types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What’s a best practice for using Partial<T>?</T>

A

Only use Partial<T> when fields are truly optional—otherwise use default values.

21
Q

What’s a best practice for using Omit<T, K>?

A

Prefer it when you need to hide or exclude certain properties from the exposed types.

22
Q

What is a potential gotcha with infer?

A

infer only works inside conditional types and can lead to obscure code if overused.

23
Q

What’s a real-world tradeoff with mapped types?

A

They reduce duplication but can introduce complexity when debugging nested generics.

24
Q

What’s a debugging tip for conditional types?

A

Break them into smaller, testable components and check inferred types using IDEs or type playgrounds.

25
How do utility types affect performance?
No runtime impact—utility types are compile-time constructs only.
26
What’s a monitoring implication of mapped types?
They can complicate stack traces when types are deeply abstracted, making error mapping harder.
27
How do these advanced types affect architecture?
They enable creation of strongly typed APIs and flexible data layers with reusable contracts.
28
What’s an example of using keyof with a function?
function getProp(obj: T, key: K): T[K] { return obj[key]; }
29
What’s an interview question about Pick vs Omit?
When would you choose `Pick` over `Omit` and vice versa?
30
What’s a performance implication of Record?
It has no runtime overhead, but too wide a type like `any` may allow unsafe operations.
31
What’s a benefit of using Readonly in APIs?
It enforces immutability, reducing side effects and increasing predictability.
32
What’s a disadvantage of overusing mapped types?
It can lead to deeply nested types that are hard to interpret and debug.
33
What’s a good use of infer with ReturnType?
To extract the return type of a function using `ReturnType`.
34
What’s a gotcha with Pick?
If K contains an invalid key, TypeScript throws an error—requires accurate key unions.
35
What’s a real-world use of Record?
Creating a dictionary-like object: `Record<'low' | 'medium' | 'high', number>` for priorities.
36
What is a debugging strategy for utility types?
Simplify complex types by checking intermediate results with helper interfaces.
37
What’s an architectural implication of utility types?
They reduce duplication and allow consistent modeling across service boundaries.
38
Can you nest utility types?
Yes, e.g., `Readonly>` is valid and creates a deeply immutable optional type.
39
What is an anti-pattern with utility types?
Creating overly abstract types that obscure the original business logic.
40
What’s the difference between Pick and Omit?
`Pick` selects properties, `Omit` excludes them—use depending on whether you're whitelisting or blacklisting.
41
What’s a good best practice for keyof?
Use it to enforce strict access to object properties via safe accessors.
42
How do you restrict inferred types with infer?
Use it in a constraint like `T extends (x: infer U) => any ? U : never`.
43
Why is Record useful in configuration objects?
It lets you map known keys to typed values in a safe, repeatable way.
44
What’s the impact of these types on system design?
They enable safer, scalable codebases by minimizing human errors in type definitions.
45
Why prefer utility types over manual typing?
They reduce redundancy, ensure consistency, and leverage TypeScript’s static analysis.
46
How does 'in' differ in mapped types vs JavaScript?
In mapped types, `in` is used to iterate over keys; in JS, it's used to check key existence.
47
What’s a potential gotcha with typeof?
`typeof` in types only works on values—not type aliases or interfaces.
48
What’s a challenge with conditional types and union types?
Distributive behavior over union types can lead to unexpected results if not handled carefully.