Home > Mobile >  TypeScript Template literals Array Join
TypeScript Template literals Array Join

Time:05-20

I found a definition for a type level Split function:

type Split<S extends string, D extends string> =
    string extends S ? string[] :
    S extends '' ? [] :
    S extends `${infer T}${D}${infer U}` ? [T, ...Split<U, D>] : [S];

Is there also a way to create a type level Join<string[], string> function, so I can use them for example for changing underscores to hyphens?

For example:

type ChangeHyphensToUnderscore<T> = { [P in keyof T & string as `${Join(Split<P, '-'>, '_')}`]: T[P] };

CodePudding user response:

Sure there is:

type Stringable = string | number | bigint | boolean | null | undefined;

type Join<A, Sep extends string = ""> = A extends [infer First, ...infer Rest] ? Rest extends [] ? `${First & Stringable}` : `${First & Stringable}${Sep}${Join<Rest, Sep>}` : "";

If you want to go as fast as Sonic you can also use TCO:

type Join<A, Sep extends string = "", R extends string = ""> = A extends [infer First, ...infer Rest] ? Join<Rest, Sep, R extends "" ? `${First & Stringable}` : `${R}${Sep}${First & Stringable}`> : R;

Here's a playground for you to play.

  • Related