読み方のヒント
矢印ボタン(または ← → キー)でページを移動できます。
型にないプロパティを書いても、エラーになります。
const melonpan: Product = {
id: "p005",
name: "メロンパン",
price: 200,
category: "pastry",
reservable: true,
color: "yellow",
};
Object literal may only specify known properties, and 'color' does not exist in type 'Product'.
| 状態 | 結果 |
|---|---|
| 必要なプロパティがない | is missing のエラー |
| 型にないプロパティがある | does not exist in type のエラー |
| プロパティの型が違う | is not assignable to のエラー |
余分なプロパティのエラーは、{ } で直接値を書いたときに出ます。打ちまちがいで reservabel と書いたときにも、このエラーで気づけます。
type ReservationItem = { productId: string; quantity: number; }; を作り、const item: ReservationItem = { productId: "p002", quantity: 2 }; と書きます。
3 / 16