読み方のヒント
矢印ボタン(または ← → キー)でページを移動できます。
1つの商品を、オブジェクトの型で表します。
type Product = {
id: string;
name: string;
price: number; // 税込の円
category: "bread" | "pastry" | "savory" | "sweets";
reservable: boolean;
};
const croissant: Product = {
id: "p002",
name: "クロワッサン",
price: 220,
category: "pastry",
reservable: true,
};
{ } の中に「プロパティの名前: 型;」を並べます。type Product = ... は、この形に Product という名前を付ける書き方で、型エイリアスと呼びます。名前は大文字で始めます。
Product の型の値には、すべてのプロパティがそろっている必要があります。
const anpan: Product = {
id: "p003",
name: "あんパン",
price: 180,
category: "bread",
};
Property 'reservable' is missing in type '{ id: string; name: string; price: number; category: "bread"; }' but required in type 'Product'.
このエラーは、「reservable がない。Product には必要」という意味です。
2 / 16