跳到主要内容

Join

介绍

实现数组的join类型, Join<T,U>接收数组T, 字符串或数值U, 返回使用U链接的T

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'
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

起点

ts
/* _____________ Your Code Here _____________ */
 
type Join<T, U> = any
 
/* _____________ Test Cases _____________ */
 
type cases = [
Expect<Equal<Join<['a', 'p', 'p', 'l', 'e'], '-'>, 'a-p-p-l-e'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Join<['Hello', 'World'], ' '>, 'Hello World'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Join<['2', '2', '2'], 1>, '21212'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Join<['o'], 'u'>, 'o'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Join<[], 'u'>, ''>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Join<['1', '1', '1']>, '1,1,1'>>,
Type 'false' does not satisfy the constraint 'true'.
Generic type 'Join' requires 2 type argument(s).
2344
2314
Type 'false' does not satisfy the constraint 'true'.
Generic type 'Join' requires 2 type argument(s).
]
ts
/* _____________ Your Code Here _____________ */
 
type Join<T, U> = any
 
/* _____________ Test Cases _____________ */
 
type cases = [
Expect<Equal<Join<['a', 'p', 'p', 'l', 'e'], '-'>, 'a-p-p-l-e'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Join<['Hello', 'World'], ' '>, 'Hello World'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Join<['2', '2', '2'], 1>, '21212'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Join<['o'], 'u'>, 'o'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Join<[], 'u'>, ''>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Join<['1', '1', '1']>, '1,1,1'>>,
Type 'false' does not satisfy the constraint 'true'.
Generic type 'Join' requires 2 type argument(s).
2344
2314
Type 'false' does not satisfy the constraint 'true'.
Generic type 'Join' requires 2 type argument(s).
]
take the challenge

解决方案

Spoiler warning // Click to reveal answer
ts
// most popular
 
type Join<T extends any[], U extends string | number> = T extends [infer F, ...infer R]
? R['length'] extends 0
? `${F & string}`
: `${F & string}${U}${Join<R, U>}`
: never
 
ts
// most popular
 
type Join<T extends any[], U extends string | number> = T extends [infer F, ...infer R]
? R['length'] extends 0
? `${F & string}`
: `${F & string}${U}${Join<R, U>}`
: never
 
ts
// my solution
 
type Join<T extends any[], U extends string|number=',', S extends string = ''> = T extends [infer F extends string|number, ...infer R]
? Join<R, U, `${S}${U}${F}`>
: S extends `${infer F extends U}${infer R}`
? R
: S;
 
ts
// my solution
 
type Join<T extends any[], U extends string|number=',', S extends string = ''> = T extends [infer F extends string|number, ...infer R]
? Join<R, U, `${S}${U}${F}`>
: S extends `${infer F extends U}${infer R}`
? R
: S;
 
view more solutions