Mapped Types
インデックス型では設定時はどのようなキーも自由に設定できてしまい、アクセス時は毎回undefined
かどうかの型チェックが必要です。入力の形式が決まっているのであればMapped Typesの使用を検討できます。
Mapped Typesは主にユニオン型と組み合わせて使います。ここにサポートする言語を定義します。
ts
typeSystemSupportLanguage = "en" | "fr" | "it" | "es";
ts
typeSystemSupportLanguage = "en" | "fr" | "it" | "es";
これをインデックス型と同じようにキーの制約として使用することができます。
ts
typeButterfly = {[key inSystemSupportLanguage ]: string;};
ts
typeButterfly = {[key inSystemSupportLanguage ]: string;};
このようにButterfly
を定義するとシステムがサポートしない言語、ここではde
が設定、使用できなくなります。
ts
constbutterflies :Butterfly = {en : "Butterfly",fr : "Papillon",it : "Farfalla",es : "Mariposa",Object literal may only specify known properties, and 'de' does not exist in type 'Butterfly'.2353Object literal may only specify known properties, and 'de' does not exist in type 'Butterfly'.: "Schmetterling", de };
ts
constbutterflies :Butterfly = {en : "Butterfly",fr : "Papillon",it : "Farfalla",es : "Mariposa",Object literal may only specify known properties, and 'de' does not exist in type 'Butterfly'.2353Object literal may only specify known properties, and 'de' does not exist in type 'Butterfly'.: "Schmetterling", de };
Mapped Typesを使ったユーティリティ型の紹介とその実現方法
プロパティを読み取り専用にするreadonly
をそのオブジェクトのすべてのプロパティに適用するReadonly<T>
というユーティリティ型があります。
📄️ Readonly<T>
全プロパティを読み取り専用にする
Readonly<T>
もこの機能で実現されています。Readonly<T>
は次のように実装されています。
ts
typeReadonly <T > = {readonly [P in keyofT ]:T [P ];};
ts
typeReadonly <T > = {readonly [P in keyofT ]:T [P ];};
keyof T
という見慣れない表現が登場しましたが、これはオブジェクトのキーをユニオン型に変更するものだと解釈してください。keyof
の詳細は型演算子をご覧ください。
📄️ keyof型演算子
keyofはオブジェクトの型からプロパティ名を型として返す型演算子です。たとえば、nameプロパティを持つ型に対して、keyofを使うと文字列リテラル型の"name"が得られます。
mapping modifier
-
を先頭につけ-readonly
とすることで、逆に読み取り専用となっているプロパティを変更可能にするMutable<T>
を作ることもできます(これはユーティリティ型にはありません)。このときの-
をmapping modifierと呼びます。
ts
typeImmutableButterfly =Readonly <Butterfly >;typeMutableButterfly = {-readonly [key inSystemSupportLanguage ]: string;};constimmutableButterfly :ImmutableButterfly = {en : "Butterfly",fr : "Papillon",it : "Farfalla",es : "Mariposa",};Cannot assign to 'en' because it is a read-only property.2540Cannot assign to 'en' because it is a read-only property.immutableButterfly .= "Schmetterling"; en constmutableButterfly :MutableButterfly = {en : "Butterfly",fr : "Papillon",it : "Farfalla",es : "Mariposa",};mutableButterfly .en = "Schmetterling"; // OK
ts
typeImmutableButterfly =Readonly <Butterfly >;typeMutableButterfly = {-readonly [key inSystemSupportLanguage ]: string;};constimmutableButterfly :ImmutableButterfly = {en : "Butterfly",fr : "Papillon",it : "Farfalla",es : "Mariposa",};Cannot assign to 'en' because it is a read-only property.2540Cannot assign to 'en' because it is a read-only property.immutableButterfly .= "Schmetterling"; en constmutableButterfly :MutableButterfly = {en : "Butterfly",fr : "Papillon",it : "Farfalla",es : "Mariposa",};mutableButterfly .en = "Schmetterling"; // OK
mapping modifier(-
)は他にもオプション修飾子の前につけて-?
とすることで、オプション修飾子を取り除くことができます。これを使うことで、Partial<T>
の逆の効果を持つRequired<T>
を実装することができます。
📄️ Partial<T>
全プロパティをオプショナルにする
📄️ Required<T>
全プロパティを必須にする
Mapped Typesには追加のプロパティが書けない
Mapped Typesは追加のプロパティが定義できません。ここは、インデックス型とは異なる点です。
ts
typeKeyValuesAndName = {[K in string]: string;A mapped type may not declare properties or methods.7061A mapped type may not declare properties or methods.: string; // 追加のプロパティ name };
ts
typeKeyValuesAndName = {[K in string]: string;A mapped type may not declare properties or methods.7061A mapped type may not declare properties or methods.: string; // 追加のプロパティ name };
追加のプロパティがある場合は、その部分をオブジェクトの型として定義し、Mapped Typesとインターセクション型を成す必要があります。
ts
typeKeyValues = {[K in string]: string;};typeName = {name : string; // 追加のプロパティ};typeKeyValuesAndName =KeyValues &Name ;
ts
typeKeyValues = {[K in string]: string;};typeName = {name : string; // 追加のプロパティ};typeKeyValuesAndName =KeyValues &Name ;
上の例は、ひとつの型にまとめることもできます。
ts
typeKeyValuesAndName = {[K in string]: string;} & {name : string; // 追加のプロパティ};
ts
typeKeyValuesAndName = {[K in string]: string;} & {name : string; // 追加のプロパティ};