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
Give it a real attempt first. Run shows which examples fail.
Saved automatically
TypeScript
Run and Submit type-check your solution against the tests. ⌘Enter runs, ⌘⇧Enter submits.