-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05310-medium-join.ts
50 lines (37 loc) · 1.48 KB
/
05310-medium-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
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
5310 - Join
-------
by Pineapple (@Pineapple0919) #medium #array
### Question
Implement the type version of Array.join, Join<T, U> takes an Array T, string or number U and returns the Array T with U stitching up.
```ts
type Res = Join<["a", "p", "p", "l", "e"], "-">; // expected to be 'a-p-p-l-e'
type Res1 = Join<["Hello", "World"], " ">; // expected to be 'Hello World'
type Res2 = Join<["2", "2", "2"], 1>; // expected to be '21212'
type Res3 = Join<["o"], "u">; // expected to be 'o'
```
> View on GitHub: https://tsch.js.org/5310
*/
/* _____________ Your Code Here _____________ */
type Join<T extends readonly string[], U extends string | number, Output extends string = ''> =
T extends [infer First, ...infer Rest]
? Rest extends readonly string[]
? First extends string
? Join<Rest, U, Output extends '' ? `${First}` : `${Output}${U}${First}`>
: never
: never
: Output
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<Join<['a', 'p', 'p', 'l', 'e'], '-'>, 'a-p-p-l-e'>>,
Expect<Equal<Join<['Hello', 'World'], ' '>, 'Hello World'>>,
Expect<Equal<Join<['2', '2', '2'], 1>, '21212'>>,
Expect<Equal<Join<['o'], 'u'>, 'o'>>,
]
/* _____________ Further Steps _____________ */
/*
> Share your solutions: https://tsch.js.org/5310/answer
> View solutions: https://tsch.js.org/5310/solutions
> More Challenges: https://tsch.js.org
*/