[Typescript] 115. Hard – Drop String()

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 ! '>>,
]
————————

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 ! '>>,
]