Skip to content
Onsite

ReplaceAll<S, From, To>

MediumP2Coding

Implement ReplaceAll<S, From, To>: replace every occurrence of the string literal From in S with To, scanning left to right without re-scanning replaced text. If From is the empty string, return S unchanged.

Tests your type must pass

type cases = [
  Expect<Equal<ReplaceAll<'foobar', 'bar', 'foo'>, 'foofoo'>>,
  Expect<Equal<ReplaceAll<'foobarbar', 'bar', ''>, 'foo'>>,
  Expect<Equal<ReplaceAll<'foobarbar', '', 'foo'>, 'foobarbar'>>,
  Expect<Equal<ReplaceAll<'barfoo', 'bar', 'foo'>, 'foofoo'>>,
  Expect<Equal<ReplaceAll<'foobarfoobar', 'ob', 'b'>, 'fobarfobar'>>,
  Expect<Equal<ReplaceAll<'foboorfoboar', 'bo', 'b'>, 'foborfobar'>>,
  Expect<Equal<ReplaceAll<'', '', ''>, ''>>,
  Expect<Equal<ReplaceAll<'aaaa', 'aa', 'b'>, 'bb'>>,
]

Hints

TypeScript
Run and Submit type-check your solution against the tests.