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: 30
11};
// 「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

TypeScript のレベル 4 のほかのコード例

← TypeScript タイピング練習のトップへ