TypeScript の Required ユーティリティ型
レベル 4228 打鍵
Required ユーティリティ型は型のすべてのプロパティを必須にします。
TypeScript のコード
// Userは3つのオプションプロパティ(?付き)を持つ1interface User { // name?: string はnameを省略できるという意味2 name?: string;3 email?: string;4 age?: number;5}// Required<User> はすべての ? を外し、3つとも必須にする6type RequiredUser = Required<User>;// ここでname、email、ageのどれかを省くとコンパイルエラーになる7const user: RequiredUser = {8 name: "Alice",9 email: "alice@example.com",10 age: 3011};// 「Alice alice@example.com 30」と表示する12console.log(user.name, user.email, user.age);実行結果
Alice alice@example.com 30
このコードでタイピング練習ができます。記録はランキングに反映されません。
このコードで使う記号
この TypeScript のコードで打つ記号と、出てくる回数です。
:× 7;× 6.× 5,× 4"× 4?× 3{× 2}× 2=× 2(× 1