0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【TypeScript】type-challenges 中級編 2・Tuple To Object 解説

Posted at

お題

タプルの値からユニオン型を生成するTupleToUnion<T>を実装する。

やりたいこと

type Tuple = ["a", 123, true];

type TupleToUnion<Tuple>; // => "a" | 123 | true

解答

type TupleToUnion<T extends any[]> = T[number];

解説

処理の流れ

  • T extends any[]
    Tをタプルに制約。
  • T[number]
    タプルにnumber型を指定することで、タプルの要素をユニオン型で取得する。

number型とは...

参考記事

number型(タプルに対して使う)

今回の問題

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?