Mapped Types Flashcards

1
Q

What are mapped types?

A

A mapped type is a generic type which uses a union of PropertyKeys (frequently created via a keyof) to iterate through keys to create a type:

A mapped type is defined using the in keyword in TypeScript, along with a type variable and an object type. Here’s a basic example of a mapped type:

type MappedType<T> = {
  [K in keyof T]: T[K];
};

Here, T is a generic type parameter, and K represents the keys of the object type T. The mapped type MappedType<T> iterates over each key in T and maps it to the corresponding value type T[K].

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

Common use cases of mapped types

A

Some common use cases of mapped types include:

1.- Making all properties of a type optional:

type Partial<T> = {
  [K in keyof T]?: T[K];
};

2.- Making all properties of a type read-only:

type Readonly<T> = {
  readonly [K in keyof T]: T[K];
};

3.- Picking a subset of properties from an existing type:

type Pick<T, K extends keyof T> = {
  [P in K]: T[P];
};

4.- Omitting a subset of properties from an existing type:

type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are mapping modifiers in TypeScript?

A

Mapping modifiers in TypeScript are a feature of mapped types that allows you to modify the properties of the object type you’re mapping over. They help add, remove, or change the attributes of properties, such as making them optional, read-only, or mutable.

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

How can you make mapped properties optional?

A

The optional modifier, denoted by ?, makes mapped properties optional. When using the resulting type, the properties can be left undefined without causing a type error.

Example:

type OptionalProperties<T> = {
  [K in keyof T]?: T[K];
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you make optional mapped properties required?

A

The required modifier, denoted by -?, makes optional properties required. When using the resulting type, the previously optional properties must now be provided and cannot be left undefined.

Example:

type RequiredProperties<T> = {
  [K in keyof T]-?: T[K];
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can you mark mapped properties as read only?

A

The read-only modifier, denoted by readonly, makes mapped properties read-only. After the object is initialized, its properties cannot be changed.

Example:

type ReadonlyProperties<T> = {
  readonly [K in keyof T]: T[K];
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you make readonly mapped properties mutable?

A

The mutable modifier, denoted by -readonly, makes read-only properties mutable. After the object is initialized, its read-only properties can now be changed.

Example:

type MutableProperties<T> = {
  -readonly [K in keyof T]: T[K];
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is key remapping?

A

Key remapping is a feature of mapped types in TypeScript that allows you to create new object types with transformed property keys based on the structure of existing types. Key remapping is achieved using the as clause in mapped types.

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

How do you use the as clause for key remapping?

A

To use the as clause for key remapping, define a mapped type with a type variable and an object type, then use the as keyword followed by an expression to transform the property keys.

Example:

type MappedWithKeyRemapping<T> = {
  [K in keyof T as `prefix_${string & K}`]: T[K];
};

In this example, the mapped type MappedWithKeyRemapping<T> iterates over each key K in the object type T and adds a prefix prefix_ to each key.

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

What is the purpose of key remapping in TypeScript?

A

Key remapping allows you to create new object types with transformed property keys based on existing types. This feature can be useful for:

1.- Renaming property keys to follow specific naming conventions.
2.- Merging or extending types with conflicting property names.
3.- Creating new types that represent the result of specific operations on objects.

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

Can you provide an example of key remapping in TypeScript?

A

Here’s an example of using key remapping to make a type with keys prefixed with “readonly”:

type ReadonlyKeysPrefixed<T> = {
  [K in keyof T as `readonly_${string & K}`]: T[K];
};

type OriginalType = {
  name: string;
  age: number;
};

type TransformedType = ReadonlyKeysPrefixed<OriginalType>;
// TransformedType is { readonly_name: string; readonly_age: number; }

In this example, the ReadonlyKeysPrefixed<T> mapped type iterates over each key K in the object type T and adds a prefix readonly_ to each key. When applied to the OriginalType, it creates a new type TransformedType with the transformed keys.

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