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'
View on GitHubts
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
/* _____________ Your Code Here _____________ */typeJoin <T ,U > = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.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'.Expect <Equal <Join <['1', '1', '1']>, '1,1,1'>>,
Generic type 'Join' requires 2 type argument(s).2344
2314Type 'false' does not satisfy the constraint 'true'.
Generic type 'Join' requires 2 type argument(s).]
take the challengets
/* _____________ Your Code Here _____________ */typeJoin <T ,U > = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.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'.Expect <Equal <Join <['1', '1', '1']>, '1,1,1'>>,
Generic type 'Join' requires 2 type argument(s).2344
2314Type 'false' does not satisfy the constraint 'true'.
Generic type 'Join' requires 2 type argument(s).]
解决方案
Spoiler warning // Click to reveal answer
ts
// most populartypeJoin <T extends any[],U extends string | number> =T extends [inferF , ...inferR ]?R ['length'] extends 0? `${F & string}`: `${F & string}${U }${Join <R ,U >}`: never
ts
// most populartypeJoin <T extends any[],U extends string | number> =T extends [inferF , ...inferR ]?R ['length'] extends 0? `${F & string}`: `${F & string}${U }${Join <R ,U >}`: never
ts
// my solutiontypeJoin <T extends any[],U extends string|number=',',S extends string = ''> =T extends [inferF extends string|number, ...inferR ]?Join <R ,U , `${S }${U }${F }`>:S extends `${inferF extendsU }${inferR }`?R :S ;
ts
// my solutiontypeJoin <T extends any[],U extends string|number=',',S extends string = ''> =T extends [inferF extends string|number, ...inferR ]?Join <R ,U , `${S }${U }${F }`>:S extends `${inferF extendsU }${inferR }`?R :S ;