上級 › Mapped typeも通常と同じ型チェックを受ける理由 › パターン5
Mapped typeも通常と同じ型チェックを受ける理由
{ [K in keyof T]?: T[K] }のようなmapped typeで生成した型は、見た目は特殊でも、実体は通常のinterfaceと同じようにプロパティごとの型チェックが行われます。
1 interface Meta { 2 title: string; 3 draft: boolean; 4 } 5 type ReadonlyMeta = { readonly [K in keyof Meta]: Meta[K] }; 6 const m: ReadonlyMeta = { title: "x", draft: _____ }; ^ 7 console.log(m);
main.ts(6,39): error TS2322: Type 'string' is not assignable to type 'boolean'.
空欄を埋めてコンパイルを通す
ReadonlyMetaはreadonlyを付けただけで、各プロパティの型はMetaのままです。
次の問題