上級 › 型注釈(: Type)はプロパティを消さずに「見えなく」する。satisfiesとの違い › パターン5
型注釈(: Type)はプロパティを消さずに「見えなく」する。satisfiesとの違い
変数に: Typeという型注釈を付けると、その型に無いプロパティへのアクセスができなくなります。
1 type Setting = { key: string }; 2 const raw = { key: "vol", value: 10 }; 3 ______________________________________ ^ 4 console.log(setting.value);
main.ts(4,21): error TS2339: Property 'value' does not exist on type 'Setting'.
空欄を埋めてコンパイルを通す
: Settingという型注釈はsettingの型をSettingに絞り込みます。satisfiesなら元の型のままvalueにアクセスできます。
次の問題