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]>;

在线演示

posted @ 2022-12-04 19:50  Laggage  阅读(25)  评论(0编辑  收藏  举报