-
Notifications
You must be signed in to change notification settings - Fork 0
/
04518-medium-fill.ts
34 lines (31 loc) · 1.2 KB
/
04518-medium-fill.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// ============= Test Cases =============
import type { Equal, Expect } from "./test-utils";
type cases = [
Expect<Equal<Fill<[], 0>, []>>,
Expect<Equal<Fill<[], 0, 0, 3>, []>>,
Expect<Equal<Fill<[1, 2, 3], 0, 0, 0>, [1, 2, 3]>>,
Expect<Equal<Fill<[1, 2, 3], 0, 2, 2>, [1, 2, 3]>>,
Expect<Equal<Fill<[1, 2, 3], 0>, [0, 0, 0]>>,
Expect<Equal<Fill<[1, 2, 3], true>, [true, true, true]>>,
Expect<Equal<Fill<[1, 2, 3], true, 0, 1>, [true, 2, 3]>>,
Expect<Equal<Fill<[1, 2, 3], true, 1, 3>, [1, true, true]>>,
Expect<Equal<Fill<[1, 2, 3], true, 10, 0>, [1, 2, 3]>>,
Expect<Equal<Fill<[1, 2, 3], true, 0, 10>, [true, true, true]>>
];
// ============= Your Code Here =============
// ref: https://github.com/type-challenges/type-challenges/issues/14102
type Fill<
T extends unknown[],
N,
Start extends number = 0,
End extends number = T["length"],
Count extends any[] = [],
NeedReplace extends boolean = Count["length"] extends Start ? true : false
// > = Count["length"] extends End
> = End extends Count["length"]
? T
: T extends [infer L, ...infer R]
? NeedReplace extends true
? [N, ...Fill<R, N, Start, End, [...Count, 0], true>]
: [L, ...Fill<R, N, Start, End, [...Count, 0]>]
: T;