問題一覧に戻る
初級基本
問題5: オプショナルプロパティ
オプショナルプロパティの定義方法を学びます。?を使うことで、プロパティを省略可能にでき、柔軟なオブジェクト構造を定義できます。
// オプショナルプロパティの定義
interface User {
name: string;
age
}
// 全てのプロパティを持つオブジェクト
let user1: User = {
name: "Alice",
email: "alice@example.com",
age: 25
};
// オプショナルプロパティを省略したオブジェクト
let user2: User = {
name: "Bob"
};