上級 › 型注釈(: Type)はプロパティを消さずに「見えなく」する。satisfiesとの違い › パターン4
型注釈(: Type)はプロパティを消さずに「見えなく」する。satisfiesとの違い
変数に: Typeという型注釈を付けると、その型に無いプロパティへのアクセスができなくなります。
1 type Shape2 = { kind: string }; 2 const raw = { kind: "circle", radius: 5 }; 3 ___________________________________ ^ 4 console.log(shape.radius);
main.ts(4,19): error TS2339: Property 'radius' does not exist on type 'Shape2'.
空欄を埋めてコンパイルを通す
: Shape2という型注釈を付けるとshape自体の型がShape2に絞られ、実際には残っているradiusへアクセスできなくなります。
次の問題