type ConstructTuple<L extends number, A extends any[] = []> = A['length'] extends L ? A : ConstructTuple<L, [...A, unknown]>
Solution by wendao-liu #35088
type ConstructTuple<L extends number, U extends unknown[] = []> = U['length'] extends L ? U : ConstructTuple<L, [...U, unknown]>
Solution by ouzexi #34101
// your answers
type ConstructTuple<L extends number, A extends any[]=[]> = L extends A ['length']
? A : ConstructTuple<L,[unknown,...A]>
Solution by pea-sys #33144
type ConstructTuple<L extends number, M extends Array<unknown> = []> = L extends M["length"]
? M
: ConstructTuple<L, [unknown, ...M]>;
Solution by gasmg #32353
type ConstructTuple<
L extends number,
_Acc extends unknown[] = []
> = _Acc['length'] extends L
? _Acc
: ConstructTuple<L, [..._Acc, unknown]>;
Solution by jjswifty #31088
type ConstructTuple<L extends number, R extends [...unknown[]] = []> = R['length'] extends L ? R : ConstructTuple<L, [...R, unknown]>
Solution by zhangqiangzgz #30214
// your answers
// 加一个数组U作为返回结果,判断U的长度,也满足@ts-expect-error的检验,递归1000的限制报错
type ConstructTuple<L extends number, U extends unknown[] = []> = Equal<
U['length'],
L
> extends true
? U
: ConstructTuple<L, [...U, unknown]>
Solution by bebusy007 #30068
type ConstructTuple<T extends number, R extends any[] = []> = R['length'] extends T ?
R : ConstructTuple<T, [...R, unknown]>;
type constructTuple = ConstructTuple<2>;
const cons: constructTuple = [1, 1];
Solution by sundial-dreams #29559
type ConstructTuple<L extends number, List extends any[] = []> = L extends 0
? []
: L extends List["length"]
? List
: ConstructTuple<L, [...List, unknown]>
Solution by Kying-star #29083
type ConstructTuple<
L extends number,
Res extends unknown[] = []
> = Res["length"] extends L ? Res : ConstructTuple<L, [...Res, unknown]>;
Solution by DoubleWoodLin #28784
// 既然要构造一个数组,就需要定义一个类型“变量”作为这个数组,L为这个数组的长度 type ConstructTuple< L extends number, T extends unknown[] = [] // 递归遍历,只要T['length']不满足L,就追加元素
= T["length"] extends L ? T : ConstructTuple<L, [...T, unknown]>;
Solution by jiaowoxiaobala #27901
type ConstructTuple<L extends number, U extends unknown[] = []> = U['length'] extends L ? U : ConstructTuple<L, [...U, unknown]>
Solution by smileboyi #27149
type ConstructTuple<Length extends number, ResultArray extends unknown[] = []> =
ResultArray['length'] extends Length ? ResultArray : ConstructTuple<Length, [...ResultArray, unknown]>
Solution by valentynpodkradylin #26244
// your answers
type ConstructTuple<L extends number, Arr extends unknown[] = []> = Arr['length'] extends L ? Arr : ConstructTuple<L, [...Arr, unknown]>
Solution by studymachiney #24400
type ConstructTuple<T extends number, Acc extends unknown[] = []> = Acc['length'] extends T
? Acc
: ConstructTuple<T, [...Acc, unknown]>
Solution by NeylonR #24371
type ConstructTuple<L extends number, _Result extends unknown[] = []> = _Result[`length`] extends L ? _Result : ConstructTuple<L, [..._Result, unknown]>;
// old way
// type TupleCreator<T extends number, _Counter extends unknown[] = []> = _Counter["length"] extends T ? _Counter : TupleCreator<T, [..._Counter, unknown]>;
// type ConstructTuple<L extends number> = TupleCreator<L>;
Solution by E-uler #23998
type ConstructTuple<L extends number, Acc extends unknown[] = []> = Acc['length'] extends L ?
Acc : ConstructTuple<L, [...Acc, unknown]>
Solution by snakeUni #23237
type ConstructTuple<L extends number, Total extends unknown[] = []> = Total['length'] extends L ? Total : ConstructTuple<L, [...Total, unknown]>
Solution by asurewall #23123
type ConstructTuple<
L extends number,
R extends any[] = []
> = R["length"] extends L ? R : ConstructTuple<L, [...R, unknown]>;
Solution by coderyoo1 #22965
// 你的答案
type ConstructTuple<L extends number, Acc extends unknown[] = []> = Acc['length'] extends L ? Acc : ConstructTuple<L, [unknown, ...Acc]>
Solution by jxhhdx #22748
type ConstructTuple<L extends number, R extends unknown[] = []> =
R['length'] extends L
? R
: ConstructTuple<L, [...R, unknown]>
Solution by drylint #22073
It's possible to construct a tuple with the maximum allowed tuple size 9999, so no need to stop at size 1000.
type TimesTen<T extends unknown[]> = [...T, ...T, ...T, ...T, ...T, ...T, ...T, ...T, ...T, ...T];
type ConstructTuple<
L extends number,
Answer extends unknown[] = []
> = `${L}` extends `${Answer["length"]}${infer Remainder}`
? Remainder extends "" ? Answer : ConstructTuple<L, TimesTen<Answer>>
: ConstructTuple<L, [unknown, ...Answer]>;
Solution by gaac510 #21209
type ConstructTuple<L extends number, U extends any[] = []> = U['length'] extends L
? U
: ConstructTuple<L,[...U, unknown]>
Solution by kfess #21077
type ConstructTuple<L extends number,Result extends Array<unknown> = []> =
L extends Result["length"] ? Result : ConstructTuple<L,[...Result,unknown]>
Solution by so11y #20680
// your answers
type ConstructTuple<L extends number, Acc extends unknown[] = [] > = Acc['length'] extends L ?
Acc
: ConstructTuple<L, [...Acc, unknown]>
Solution by Quanzhitong #20392
// your answers
type ConstructTuple<L extends number,A extends any[] = []> = A['length'] extends L ? A : A extends [...infer R] ?
ConstructTuple<L,[...R,unknown]> : A
Solution by YqxLzx #20233
// your answers
type ConstructTuple<n, Arr extends unknown[] = []> = Arr["length"] extends n
? Arr
: ConstructTuple<n, [...Arr, unknown]>;
Solution by fengjinlong #20091
type ConstructTuple<L extends number, T extends unknown[] = []> =
T['length'] extends L
? T
: ConstructTuple<L, [unknown, ...T]>
We have build tuple many times in the previous challenges. The last test case seems expect an recursion depth error, so we just write a naive version of tuple for it.
// @ts-expect-error
Expect<Equal<ConstructTuple<1000>['length'], 1000>>,
Solution by zhaoyao91 #19715
type ConstructTuple<L extends number, N extends unknown[] = []> = N['length'] extends L ? N : ConstructTuple<L, [...N, unknown]>;
Solution by CaoXueLiang #18673
type ConstructTuple<L extends number, Arr extends unknown[] = []> = Arr['length'] extends L ? Arr : ConstructTuple<L, [...Arr, unknown]>
Solution by LemonNekoGH #18328