[Typescript] 80. Medium - Construct Tuple

Construct a tuple with a given length.

For example

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

 

/* _____________ Your Code Here _____________ */

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


/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<ConstructTuple<0>, []>>,
  Expect<Equal<ConstructTuple<2>, [unknown, unknown]>>,
  Expect<Equal<ConstructTuple<999>['length'], 999>>,
  // @ts-expect-error
  Expect<Equal<ConstructTuple<1000>['length'], 1000>>,
]

 

posted @   Zhentiw  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2020-10-30 [Kotlin] Try resource, use {} block
2020-10-30 [Kotlin] Try catch & Throw
2020-10-30 [Kotlin] mutableList with toList
2018-10-30 [React] Use React.memo with a Function Component to get PureComponent Behavior
2018-10-30 [Javascript] Use a custom sort function on an Array in Javascript
2017-10-30 [Anuglar & NgRx] StoreRouterConnectingModule
2017-10-30 [Unit testing] data-test attr FTW
点击右上角即可分享
微信分享提示