TypeScript の テンプレートリテラル型

レベル 4240 打鍵

テンプレートリテラル型は文字列リテラル型を新しいパターンに結合します。

TypeScript のコード

// 3つの色の名前
1type Color = "red" | "blue" | "green";
// 3つのサイズの名前
2type Size = "small" | "medium" | "large";
// すべてのSize-Colorの組み合わせ("small-red" など3 × 3 = 9通り)
3type ProductCode = `${Size}-${Color}`;
// 有効なコードです(順序が逆の "red-small" はエラー)
4const code1: ProductCode = "small-red";
5const code2: ProductCode = "large-blue";
// small-red、続いてlarge-blueを表示します
6console.log(code1);
7console.log(code2);

実行結果

small-red
large-blue
このコードでタイピング練習する

このコードでタイピング練習ができます。記録はランキングに反映されません。

このコードで使う記号

この TypeScript のコードで打つ記号と、出てくる回数です。

  • "× 16
  • ;× 7
  • =× 5
  • |× 4
  • -× 3
  • :× 2
  • .× 2
  • (× 2
  • )× 2
  • {× 2

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

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