generated at
Mapped Types Modifiers


揺れがある
「Mapped Type Modifiers」とか ref
「Mapping Modifiers」とか ref
「Modifier Type」とか ref
readonly ? のことをmodifier(修飾子)と呼んでて、
それをMapped Typesの定義時に、付与したり、除去したりできるので
「Mapped Types Modifiers」の様に呼んだりする


以下の様に、constraint typeに keyof が付いている時に使える
[] の中で keyof を付けている
ts
type A<T> = { [P in keyof T]: string };
extends の中で、 keyof の制約がある
ts
type Pick<T, K extends keyof T> = { [P in K]: T[P]; }





readonly ? を付ける
例えば、全てのpropertyに ? を付けるPartial<R>
ts
type Partial<T> = { [P in keyof T]?: T[P]; };
こうなる
ts
type A = Partial<{ a: number; b: number }>; // 以下と同じ // type A = { // a?: number | undefined; // b?: number | undefined; // };
ただの [..]? じゃなくて、 [..]+? とも書けるが、全く同じ意味になる
なんで用意しているんだろう #??


? を取り除く
- を付ける
例えば、全てのpropertyを必須にするRequired<T>
ts
type Required<T> = { [P in keyof T]-?: T[P]; };
こうなる
ts
type A = Required<{ a?: number; b?: number | undefined }>; // 以下と同じ // type A = { a: number; b: number; };


readonly を取り除く
- を付ける
ts
type Mutable<T> = { -readonly [P in keyof T]: T[P]; };



参考