DropChar
介绍
从字符串中剔除指定字符。
例如:
ts
type Butterfly = DropChar<' b u t t e r f l y ! ', ' '> // 'butterfly!'
View on GitHubts
type Butterfly = DropChar<' b u t t e r f l y ! ', ' '> // 'butterfly!'
起点
ts
/* _____________ Your Code Here _____________ */typeDropChar <S ,C > = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <DropChar <'butter fly!', ''>, 'butterfly!'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <DropChar <'butter fly!', ' '>, 'butterfly!'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <DropChar <'butter fly!', '!'>, 'butter fly'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <DropChar <' butter fly! ', ' '>, 'butterfly!'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <DropChar <' b u t t e r f l y ! ', ' '>, 'butterfly!'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <DropChar <' b u t t e r f l y ! ', 'b'>, ' u t t e r f l y ! '>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <DropChar <' b u t t e r f l y ! ', 't'>, ' b u e r f l y ! '>>,]
take the challengets
/* _____________ Your Code Here _____________ */typeDropChar <S ,C > = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <DropChar <'butter fly!', ''>, 'butterfly!'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <DropChar <'butter fly!', ' '>, 'butterfly!'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <DropChar <'butter fly!', '!'>, 'butter fly'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <DropChar <' butter fly! ', ' '>, 'butterfly!'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <DropChar <' b u t t e r f l y ! ', ' '>, 'butterfly!'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <DropChar <' b u t t e r f l y ! ', 'b'>, ' u t t e r f l y ! '>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <DropChar <' b u t t e r f l y ! ', 't'>, ' b u e r f l y ! '>>,]
解决方案
Spoiler warning // Click to reveal answer
ts
// most populartypeDropChar <S ,C extends string> =S extends `${inferL }${C }${inferR }` ?DropChar <`${L }${R }`,C > :S ;/*** 存在的问题, 在ts的playground(v5.8.2)中, 当处理DropChar<' butter fly!', ''>时, 有潜在警告: Type instantiation is excessively deep and possibly infinite.(2589)*/
ts
// most populartypeDropChar <S ,C extends string> =S extends `${inferL }${C }${inferR }` ?DropChar <`${L }${R }`,C > :S ;/*** 存在的问题, 在ts的playground(v5.8.2)中, 当处理DropChar<' butter fly!', ''>时, 有潜在警告: Type instantiation is excessively deep and possibly infinite.(2589)*/
ts
/*** 问题:* 1.'' 和' '是俩类型, 那如何判断相等呢?* 2.我的初始思路也是按照most popular, 但是遇到了1给否了.接下来的思路, 是转换成数组, 递归判断是否相等* 2.1 如果相等, 返回的元素中就剔除这个; 如果不相等,返回的元素中就包含这个.* 2.2 但是判断相等依然无法解决.* 3.将数组转换为字符串*/typeArrToStr <A ,S extends string = ''> =A extends [inferF extends string, ...inferRest ]?ArrToStr <Rest , `${S }${F }`>:S ;typeStrToArr <S ,T extends any[] = []> =S extends `${inferF }${inferRest }`? [...T ,F , ...StrToArr <Rest ,T >]:T ;
ts
/*** 问题:* 1.'' 和' '是俩类型, 那如何判断相等呢?* 2.我的初始思路也是按照most popular, 但是遇到了1给否了.接下来的思路, 是转换成数组, 递归判断是否相等* 2.1 如果相等, 返回的元素中就剔除这个; 如果不相等,返回的元素中就包含这个.* 2.2 但是判断相等依然无法解决.* 3.将数组转换为字符串*/typeArrToStr <A ,S extends string = ''> =A extends [inferF extends string, ...inferRest ]?ArrToStr <Rest , `${S }${F }`>:S ;typeStrToArr <S ,T extends any[] = []> =S extends `${inferF }${inferRest }`? [...T ,F , ...StrToArr <Rest ,T >]:T ;