-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedium-replace.ts
More file actions
30 lines (26 loc) · 915 Bytes
/
medium-replace.ts
File metadata and controls
30 lines (26 loc) · 915 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/* _____________ Your Code Here _____________ */
type Replace<
S extends string,
From extends string,
To extends string,
> = From extends ''
? S
: S extends `${infer L}${From}${infer R}`
? `${L}${To}${R}`
: S;
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils';
type cases = [
Expect<Equal<Replace<'foobar', 'bar', 'foo'>, 'foofoo'>>,
Expect<Equal<Replace<'foobarbar', 'bar', 'foo'>, 'foofoobar'>>,
Expect<Equal<Replace<'foobarbar', '', 'foo'>, 'foobarbar'>>,
Expect<Equal<Replace<'foobarbar', 'bar', ''>, 'foobar'>>,
Expect<Equal<Replace<'foobarbar', 'bra', 'foo'>, 'foobarbar'>>,
Expect<Equal<Replace<'', '', ''>, ''>>,
];
/* _____________ Further Steps _____________ */
/*
> Share your solutions: https://tsch.js.org/116/answer
> View solutions: https://tsch.js.org/116/solutions
> More Challenges: https://tsch.js.org
*/