題目
中文
實現類型版本的 Array.join, Join<T, U> 接受數組 T 和 string 或者 number 類型 U 作爲泛型參數, 並返回 U 連接數組 T 後的字符串.
English
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.
答案
type Stringify = string | number | bigint | boolean | null | undefined;
type Join<
T extends Stringify[],
U extends Stringify,
S extends Stringify = ''
> = T extends [infer L extends string, ...infer R extends string[]]
? Join<R, U, '' extends S ? `${L}` : `${S}${U}${L}`>
: S;