首页 > 其他分享 >Typescript类型体操 - Join

Typescript类型体操 - Join

时间:2022-11-08 01:55:32浏览次数:71  
标签:Typescript Join string number extends 体操 Array type

题目

中文

实现类型版本的 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;

在线演示

标签:Typescript,Join,string,number,extends,体操,Array,type
From: https://www.cnblogs.com/laggage/p/type-challenge-join.html

相关文章