? means the field may be missing. readonly blocks assignment.
Goal
Log a missing note and a frozen city.
interface Sale {
city: string;
units: number;
note?: string;
}
const a: Sale = { city: "Nairobi", units: 12 };
const b: Sale = { city: "Mombasa", units: 8, note: "coast" };
console.log(a.note);
console.log(b.note);interface Sale {
readonly city: string;
units: number;
}
const row: Sale = { city: "Kisumu", units: 5 };
console.log(row.city);function label(city?: string): string {
return city ?? "Nairobi";
}
console.log(label());
console.log(label("Eldoret"));const cities: readonly string[] = ["Nairobi", "Nakuru"];
console.log(cities[1]);