-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00545-hard-printf.ts
39 lines (31 loc) · 1.24 KB
/
00545-hard-printf.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
34
35
36
37
38
39
// ============= Test Cases =============
import type { Equal, Expect } from './test-utils'
type cases = [
Expect<Equal<Format<'abc'>, string>>,
Expect<Equal<Format<'a%sbc'>, (s1: string) => string>>,
Expect<Equal<Format<'a%dbc'>, (d1: number) => string>>,
Expect<Equal<Format<'a%%dbc'>, string>>,
Expect<Equal<Format<'a%%%dbc'>, (d1: number) => string>>,
Expect<Equal<Format<'a%dbc%s'>, (d1: number) => (s1: string) => string>>,
]
// ============= Your Code Here =============
type GetFormatters<T extends string> = T extends `${string}%${infer Formatter}${infer Rest}`
? Formatter extends "%" ? GetFormatters<Rest> : [Formatter, ...GetFormatters<Rest>]
: []
type Mapper = {
s: string;
d: number;
}
type Format<T extends string, Formatters = GetFormatters<T>> = Formatters extends [infer First, ...infer Rest]
? First extends keyof Mapper ? (param: Mapper[First]) => Format<T, Rest> : never
: string
type test1 = GetFormatters<'a%sbc'>
type test2 = GetFormatters<'a%dbc'>
type test3 = GetFormatters<'a%%dbc'>
type test4 = GetFormatters<'a%%%dbc'>
type test5 = GetFormatters<'a%dbc%s'>
type test6 = Format<'abc'>
type test7 = Format<'a%sbc'>
type test8 = Format<'a%dbc'>
type test9 = Format<'a%%%dbc'>
type test10 = Format<'a%dbc%s'>