Typescript類型體操 - ConstructTuple

題目

中文

構造一個給定長度的元組。

例如

type result = ConstructTuple<2> // 期望得到 [unknown, unkonwn]

English

Construct a tuple with a given length.

For example

type result = ConstructTuple<2> // expect to be [unknown, unkonwn]

答案

type ConstructTuple<
  L extends number,
  K extends any[] = []
> = K['length'] extends L ? K : ConstructTuple<L, [unknown, ...K]>;

在線演示

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章