feat(question): add #34979 - Construct Tuple By Length

This commit is contained in:
minhwan Kim 2024-11-07 15:32:29 +00:00
parent c42690e211
commit e6811109d5
4 changed files with 21 additions and 0 deletions

View File

@ -0,0 +1,5 @@
Given a generic type `T` and a generic number type `Length extends number`, returns a tuple of type `T` with a length of `Length`.
```ts
type result = ConstructTupleByLength<number, 2>; // [number, number]
```

View File

@ -0,0 +1,8 @@
difficulty: medium
title: Construct Tuple By Length
tags: tuple, conditional type
related: 7544
author:
github: keepmhwn
name: minhwan Kim

View File

@ -0,0 +1 @@
type ConstructTupleByLength<T, Length extends number> = any;

View File

@ -0,0 +1,7 @@
import type { Equal, Expect } from '@type-challenges/utils'
import { ExpectFalse, NotEqual } from '@type-challenges/utils'
type cases = [
Expect<Equal<ConstructTupleByLength<unknown, 0>, []>>,
Expect<Equal<ConstructTupleByLength<number | string, 2>, [number | string, number | string]>>,
]