Mapped typeも通常と同じ型チェックを受ける理由
{ [K in keyof T]?: T[K] }のようなmapped typeで生成した型は、見た目は特殊でも、実体は通常のinterfaceと同じようにプロパティごとの型チェックが行われます。生成された型だからといって検査が緩くなることはありません。
エラーメッセージの読み方
main.ts(6,47): error TS2322: Type 'string' is not assignable to type 'number'.
main.ts- ファイル名
6- 行番号
47- 列番号 — この位置でtscが検査に失敗しました
TS2322- エラーコード — 検索するとTypeScriptの解説が見つかります
Type 'string' is not assignable to type 'number'.- 内容 — 期待していたもの、または受け付けられなかったもの
このエラーが出る典型パターン
パターン1
1 interface Config { 2 host: string; 3 port: number; 4 } 5 type PartialConfig = { [K in keyof Config]?: Config[K] }; 6 const c: PartialConfig = { host: "localhost", port: "8080" }; ^ 7 console.log(c);
main.ts(6,47): error TS2322: Type 'string' is not assignable to type 'number'.
PartialConfigはConfigの各プロパティをoptionalにしただけで、型自体はConfig[K]のまま維持されます。portには文字列を代入できません。
直し方: "8080" を 8080 にします。
広告
広告スロット(未設定)
パターン2
1 interface Flags { 2 active: boolean; 3 count: number; 4 } 5 type ReadonlyFlags = { readonly [K in keyof Flags]: Flags[K] }; 6 const f: ReadonlyFlags = { active: true, count: "3" }; ^ 7 console.log(f);
main.ts(6,42): error TS2322: Type 'string' is not assignable to type 'number'.
ReadonlyFlagsはreadonlyを付けただけで、各プロパティの型はFlagsのままです。countには数値しか入りません。
直し方: "3" を 3 にします。
パターン3
1 interface Settings { 2 volume: number; 3 muted: boolean; 4 } 5 type PartialSettings = { [K in keyof Settings]?: Settings[K] }; 6 const s: PartialSettings = { volume: 10, muted: "yes" }; ^ 7 console.log(s);
main.ts(6,42): error TS2322: Type 'string' is not assignable to type 'boolean | undefined'.
PartialSettingsのmutedはboolean型のままです。文字列の"yes"は代入できません。
直し方: "yes" を true にします。
よくある誤解
「mapped typeは動的に生成される特別な型だから、通常の型チェックの対象外では」という予想は誤りです。tscはmapped typeを展開した結果としての具体的なプロパティ型を、通常のオブジェクト型と同じ精度でチェックします。
まとめ
Mapped typeも通常と同じ型チェックを受ける理由は上級でつまずきやすい項目です。上の3パターンを実際に手で直すと、エラーメッセージのどこを読めばよいかが掴めます。