type Join<T, U extends string | number = ',', Res extends string = ''> = T extends [infer F extends string, ...infer R extends string[]]
? Join<R, U, Res extends '' ? `${F}` : `${Res}${U}${F}`> : Res
Solution by tani-fumiya-ktc #35202
type Join<T, U extends string | number = ',', S extends string = ''> = T extends [infer F, ...infer Rest] ? Join<Rest, U, `${S}${S extends '' ? '' : U}${F & string}`> : S
type Join<T, U extends string | number = ',', S extends string = ''> = T extends [infer F extends string, ...infer Rest] ? Join<Rest, U, `${S}${S extends '' ? '' : U}${F}`> : S
Solution by wendao-liu #35077
// your answers
type Join<T, U = ','> = T extends [infer P, ...infer L] ? `${P & string}${L['length'] extends 0 ? '' : U & (string | number)}${Join<L, U> & string}`: ''
Solution by gobielJonathan #34486
type Join<T extends any[], U extends number | string = ','> = T extends [] ? '' :
T extends [infer R, ...infer rest] ?
rest['length'] extends 0 ? R & string :
`${R & string}${U}${Join<rest, U>}` : never
Solution by ouzexi #34093
Try it in TS Playground with additional test case
type Stringifiable = string | number | bigint | boolean | null | undefined;
type Join<
T extends readonly Stringifiable[],
U extends Stringifiable = ",",
> = T extends []
? ""
: T extends [infer First extends Stringifiable]
? `${First}`
: T extends [
infer First extends Stringifiable,
infer Second extends Stringifiable,
...infer Rest extends Stringifiable[],
]
? Join<[`${First}${U}${Second}`, ...Rest], U>
: string;
Solution by PinkChampagne17 #33930
type Join<T, U extends string | number, V extends string = ""> = T extends []
? V extends ""
? ""
: V
: T extends [infer F, ...infer R]
? R extends []
? Join<R, U, `${V}${F & string}`>
: Join<R, U, `${V}${F & string}${U}`>
: never;
Solution by gasmg #32253
type Join<T extends unknown[], U extends string|number, R extends any = ''> = T extends [infer F, ...infer Rest] ? Rest extends [] ?
Join<Rest, U, `${R}${F}`> : Join<Rest, U, `${R}${F}${U}`> : `${R}`
Solution by Zhen-code #32040
type Join<T extends any[], U extends string | number> = T extends [
infer F extends string,
...infer R
]
? R["length"] extends 0
? `${F}`
: `${F}${U}${Join<R, U>}`
: "";
Solution by sunsunmonkey #31917
// your answers
type SupportedType = string | number | bigint | boolean | null | undefined
type Join<T extends SupportedType[], U extends SupportedType> = T extends [infer F extends SupportedType, ...infer Rest extends SupportedType[]] ? Rest extends [] ? `${F}` : `${F}${U}${Join<Rest, U>}` : ''
Solution by Kakeru-Miyazaki #30893
type Join<T extends any[], U extends string | number> =
T extends [infer F, ...infer R]
? `${F & string}${R extends [] ? '' : U}${Join<R, U>}`
: ''
Solution by matallui #30812
type Join<T extends unknown[], U extends string | number> = T extends [infer F extends string, ...infer R]
? `${F}${R extends [] ? '' : U}${Join<R, U>}`
: ''
Solution by wangly19 #30756
type Join<T extends any[], U extends string | number> = T extends [infer A, ...infer B] ? `${A & string}${ Join<B, U> extends '' ? '' : U}${Join<B, U>}` : ''
Solution by dreamluo-plus #30663
type Join<T extends Array<string|number>, U extends string|number,F extends string = ''> = T extends [] ? '': T extends [infer S extends string,...infer O extends string[]] ? O extends [] ? `${F}${S}`: Join<O,U,`${F}${S}${U}`>:never;
Solution by jiangxd2016 #30553
type Join<T extends unknown[], U extends string | number> = T extends [infer F extends string, ...infer R]
? `${F}${R extends [] ? '' : U}${Join<R, U>}`
: ''
Solution by iclaxton #30343
type Join<T extends unknown[], U extends string | number, S extends string = ''> =
T extends [infer A extends string, ...infer Rest]
?
Rest extends []
?
`${S}${A}`
: Join<Rest, U, `${S}${A}${U}`>
: S
Solution by zhangqiangzgz #30201
type Join<T extends any[], U extends string | number> = T["length"] extends 1
? `${T[0]}`
: T extends [infer S extends string, ...infer Tail]
? Tail extends string[]
? Join<Tail, U> extends string
? `${S}${U}${Join<Tail, U>}`
: never
: `${S}`
: T
Solution by Kying-star #29042
type Join<T, U extends number | string> = T extends [infer F, ...infer R]
? R extends []
? `${F & string}`
: `${F & string}${U}${Join<R, U>}`
: "";
Solution by DoubleWoodLin #28780
// your answers
type Join<T, U extends string | number> =
T extends [infer S extends string, ...infer Tail] ?
Tail["length"] extends 0 ? `${S}`
:`${S}${U}${Join<Tail, U>}`
:never
Solution by kerolossamir165 #28467
// 定义一个类型"变量"R存储拼接后的字符
type Join<T, U extends string | number, R extends string = ""> = T extends [
// 取出第一个字符
infer F,
...infer Rest
]
// 递归拼接
? Join<Rest, U, `${R}${R extends "" ? "" : U}${F & string}`>
: R;
Solution by jiaowoxiaobala #27984
type Join<T, U extends string | number, R extends string = ""> = T extends [
infer F,
...infer Rest
]
? Join<Rest, U, ${R}${R extends "" ? "" : U}${F & string}
>
: R;
Solution by jiaowoxiaobala #27869
type Join<T extends unknown[], S extends string, Result extends string = ''> = T['length'] extends 1
? ${Result}${T[0] & string}
: T extends [infer Pre, ...infer Next]
? Join<Next, S, ${Result}${Pre & string}${S}
> : Result
Solution by jiechliu #27804
// your answers
type Join<T extends unknown[], K extends string | number> = T extends [infer First extends string, ...infer Rest] ? Rest['length'] extends 0 ? `${First}${Join<Rest, K>}` : `${First}${K}${Join<Rest, K>}` : '';
Solution by Hencky #27638
type Join<
T extends string[],
U extends string | number,
Result extends string = ""
> = T extends [infer F extends string, ...infer R extends string[]]
? Join<R, U, Result extends "" ? F : `${Result}${U}${F}`>
: Result;
Solution by alythobani #27610
type Join<T extends string[], S extends string> = T extends [
infer Start extends string,
...infer Rest
]
? `${Start}${Rest extends [string] ? `${S}${Join<Rest, S>}` : ""}`
: "";
Solution by slemchik03 #27574
type Join<T extends (number | string)[], U extends string | number, _Acc extends string = ''> =
T extends [infer Char extends string | number, ...infer Rest extends (string | number)[]] ?
Join<Rest, U, `${_Acc}${Char}${Rest extends [] ? '' : U}`> : _Acc
Solution by jjswifty #27539
type Join<T extends Array<number | string>, U extends number | string, S extends string = ''> =
T extends [infer F, ...infer R extends Array<number | string>]
? Join<R, U, `${S}${F & string}${R extends [] ? '' : `${U}`}`> : S
Solution by smileboyi #27109
type Join<A extends any[], I extends number | string> = A extends [infer F, ...infer L] ? L["length"] extends 0 ? `${F & string}` : `${F & string}${I}${Join<L, I>}` : never
Solution by 20yuteo #26977
type ToString<T>= T extends string ? T : never;
type ToStringTuple<T>= T extends string[] ? T : never;
type Join<T extends string[], U extends string | number> =
T extends [infer F, ...infer Rest] ?
Rest extends [] ?
`${ToString<F>}` :
`${ToString<F>}${U}${Join<ToStringTuple<Rest>, U>}` :
'';
Solution by kakasoo #26441
type Join<T extends string[], U extends string | number> = T extends [] ? '' : T extends [string] ? `${T[0]}` : T extends [string, ...infer Args] ? Args extends string[] ? `${T[0]}${U}${Join<Args,U>}`: never : never
Solution by ScarboroughCoral #26340
type Join<TArray extends string[], Divider extends string | number> = TArray extends [
infer Head extends string,
...infer Tail extends [] | string[],
]
? IsEmptyArray<Tail> extends true
? `${Head}`
: `${Head}${Divider}${Join<Tail, Divider>}`
: ''
Solution by valentynpodkradylin #26243