TypeScript の 型の異なるPromise.all

レベル 4195 打鍵

Promise.allは複数のPromiseをまとめて実行し、タプルを返すので、各結果はそれぞれの型を保ちます。

TypeScript のコード

1const double = async (n: number) => n * 2;
2const label = async () => "ok";
3async function run() {
// Promise.allは両方のPromiseを待ち、タプルが型を保つので、nはnumber、sはstringになります
4 const [n, s] = await Promise.all([double(4), label()]);
// sはstringだと分かっているので、toUpperCaseを使えます
5 console.log(n, s.toUpperCase());
6}
7run();

実行結果

8 OK
このコードでタイピング練習する

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

このコードで使う記号

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

  • (× 9
  • )× 9
  • ;× 5
  • =× 5
  • ,× 3
  • .× 3
  • "× 2
  • [× 2
  • ]× 2
  • >× 2

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

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