-
Notifications
You must be signed in to change notification settings - Fork 0
/
join.ts
36 lines (33 loc) · 1.01 KB
/
join.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
35
36
// https://github.com/type-challenges/type-challenges/blob/84360326c02461ae63b55c6d396ad1679917c430/questions/847-hard-string-join/README.md
type Join<
Del extends string,
Arr extends string[],
Res extends string = ""
> =
Arr extends [infer Head, ...infer Tail]
? Head extends string
? Tail extends string[]
? Join<Del, Tail, `${Res}${Head}${Tail extends [] ? "" : Del}`>
: never
: never
: Res
;
export declare function join<Del extends string>(delimiter: Del):
<Arr extends string[]>(...parts: Arr) => Join<Del, Arr>
;
/*
Alternative implementation for Join that starts working from the end
type Join<
Del extends string,
Arr extends string[],
Res extends string = ""
> =
Arr extends [...infer Init, infer Last]
? Last extends string
? Init extends string[]
? Join<Del, Init, `${Last}${Res extends "" ? "" : Del}${Res}`>
: never
: never
: Res
;
*/