跳到主要内容

DropChar

介绍

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

例如:

ts
type Butterfly = DropChar<' b u t t e r f l y ! ', ' '> // 'butterfly!'
ts
type Butterfly = DropChar<' b u t t e r f l y ! ', ' '> // 'butterfly!'
View on GitHub

起点

ts
/* _____________ Your Code Here _____________ */
 
type DropChar<S, C> = any
 
/* _____________ Test Cases _____________ */
 
type cases = [
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 ! '>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
 
ts
/* _____________ Your Code Here _____________ */
 
type DropChar<S, C> = any
 
/* _____________ Test Cases _____________ */
 
type cases = [
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 ! '>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
 
take the challenge

解决方案

Spoiler warning // Click to reveal answer
ts
// most popular
 
type DropChar<S, C extends string> = S extends `${infer L}${C}${infer R}` ? DropChar<`${L}${R}`, C> : S;
 
/**
* 存在的问题, 在ts的playground(v5.8.2)中, 当处理DropChar<' butter fly!', ''>时, 有潜在警告: Type instantiation is excessively deep and possibly infinite.(2589)
*/
ts
// most popular
 
type DropChar<S, C extends string> = S extends `${infer L}${C}${infer R}` ? 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.将数组转换为字符串
*/
type ArrToStr<A, S extends string = ''> = A extends [infer F extends string, ...infer Rest]
? ArrToStr<Rest, `${S}${F}`>
: S;
 
type StrToArr<S, T extends any[] = []> = S extends `${infer F}${infer Rest}`
? [...T, F, ...StrToArr<Rest, T>]
: T;
 
ts
/**
* 问题:
* 1.'' 和' '是俩类型, 那如何判断相等呢?
* 2.我的初始思路也是按照most popular, 但是遇到了1给否了.接下来的思路, 是转换成数组, 递归判断是否相等
* 2.1 如果相等, 返回的元素中就剔除这个; 如果不相等,返回的元素中就包含这个.
* 2.2 但是判断相等依然无法解决.
* 3.将数组转换为字符串
*/
type ArrToStr<A, S extends string = ''> = A extends [infer F extends string, ...infer Rest]
? ArrToStr<Rest, `${S}${F}`>
: S;
 
type StrToArr<S, T extends any[] = []> = S extends `${infer F}${infer Rest}`
? [...T, F, ...StrToArr<Rest, T>]
: T;
 
view more solutions