-
Notifications
You must be signed in to change notification settings - Fork 0
/
23 ~ Exercise: SplitUrl.ts
73 lines (61 loc) · 1.9 KB
/
23 ~ Exercise: SplitUrl.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Task: convert a url into its parts and retrieve url parameters
const homeUrl = "/"
const contactUrl = "/about-us/contact"
const companyUrl = "/company/:companyName"
const companyJobUrl = "/company/:companyName/job/:jobId"
// Some Things you might need:
// You can extend a tuple
type Tuple = [string, number]
type ExtendTuple = [boolean, ...Tuple]
// type ExtendTuple = [boolean, string, number]
type X = "foo" | "bar" | "foobar"
// You can rename in mapped types using 'as'
type Renamed = {
[Key in X as `renamed-${Key}`]: string
}
// type Renamed = {
// "renamed-foo": string;
// "renamed-bar": string;
// "renamed-foobar": string;
// }
// Rename to "never" to remove a field
type RenamedAndRemoveSome = {
[Key in X as Key extends `foo${infer _}` ? `renamed-${Key}` : never]: string
}
// type RenamedAndRemoveSome = {
// "renamed-foo": string;
// "renamed-foobar": string;
// }
/*
*
*
*
*/
type UrlParts<Url extends string> = any
// type HomeUrlParts = [""]
type HomeUrlParts = UrlParts<typeof homeUrl>
// type ContactUrlParts = ["about-us", "contact"]
type ContactUrlParts = UrlParts<typeof contactUrl>
// type CompanyUrlParts = ["company", ":companyName"]
type CompanyUrlParts = UrlParts<typeof companyUrl>
// type CompanyJobUrlParts = ["company", ":companyName", "job", ":jobId"]
type CompanyJobUrlParts = UrlParts<typeof companyJobUrl>
/*
*
*
*
*/
type UrlParameters<Url extends string> = any
// type UrlParametersForHome = {}
type UrlParametersForHome = UrlParameters<typeof homeUrl>
// type UrlParametersForContact = {}
type UrlParametersForContact = UrlParameters<typeof contactUrl>
// type UrlParametersForCompanyUrl = {
// companyName: string;
// }
type UrlParametersForCompanyUrl = UrlParameters<typeof companyUrl>
// type UrlParametersForCompanyJobUrl = {
// companyName: string;
// jobId: string;
// }
type UrlParametersForCompanyJobUrl = UrlParameters<typeof companyJobUrl>