-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02059-hard-drop-string.ts
33 lines (26 loc) · 1.28 KB
/
02059-hard-drop-string.ts
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
31
32
33
// ============= Test Cases =============
import type { Equal, Expect } from './test-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 ! '>>,
]
// ============= Your Code Here =============
type StringToUnion<T extends string> = T extends `${infer First}${infer Rest}` ?
First | StringToUnion<Rest>
:
T
type Test1 = StringToUnion<'Hello'>
type DropString<S extends string, R extends string, T = StringToUnion<R>> = S extends `${infer First}${infer Rest}`
? First extends T
? DropString<Rest, R>
: `${First}${DropString<Rest, R>}`
: S
type Test2 = DropString<'butter fly', 'but'>