Drop the specified chars from a string.
For example:
type Butterfly = DropString<'foobar!', 'fb'> // 'ooar!'
/* _____________ Your Code Here _____________ */
type StringToUnion<S> = S extends `${infer F}${infer RT}` ? F | StringToUnion<RT>: never;
type DropString<S, R> = R extends '' ? S : S extends `${infer F}${infer RT}`
? F extends StringToUnion<R> ? DropString<RT, R>: `${F}${ DropString<RT, R>}`
: S;
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<DropString<'butter fly!', ''>, 'butter fly!'>>,
Expect<Equal<DropString<'butter fly!', ' '>, 'butterfly!'>>,
Expect<Equal<DropString<'butter fly!', 'but'>, 'er fly!'>>,
Expect<Equal<DropString<' b u t t e r f l y ! ', 'but'>, ' e r f l y ! '>>,
Expect<Equal<DropString<' butter fly! ', ' '>, 'butterfly!'>>,
Expect<Equal<DropString<' b u t t e r f l y ! ', ' '>, 'butterfly!'>>,
Expect<Equal<DropString<' b u t t e r f l y ! ', 'but'>, ' e r f l y ! '>>,
Expect<Equal<DropString<' b u t t e r f l y ! ', 'tub'>, ' e r f l y ! '>>,
Expect<Equal<DropString<' b u t t e r f l y ! ', 'b'>, ' u t t e r f l y ! '>>,
Expect<Equal<DropString<' b u t t e r f l y ! ', 't'>, ' b u e r f l y ! '>>,
]
标签:Typescript,String,_____________,DropString,115,extends,Expect,type,infer From: https://www.cnblogs.com/Answer1215/p/16923989.html