Typescript类型体操 - DropChar

题目

中文

从字符串中剔除指定字符。

例如:

type Butterfly = DropChar<' b u t t e r f l y ! ', ' '>; // 'butterfly!'

English

Drop a specified char from a string.

For example:

type Butterfly = DropChar<' b u t t e r f l y ! ', ' '>; // 'butterfly!'

答案

type DropChar<
    S extends string,
    C extends string
> = S extends `${infer L}${infer R}`
    ? `${C extends L ? '' : L}${DropChar<R, C>}`
    : S;

在线演示

posted @ 2022-09-19 01:00  Laggage  阅读(34)  评论(0编辑  收藏  举报