-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterfaces.ts
More file actions
70 lines (60 loc) · 2.09 KB
/
interfaces.ts
File metadata and controls
70 lines (60 loc) · 2.09 KB
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
import * as React from "react"
import { Exactly, Literal, PartPartial } from "./type-utils"
export interface RouterStore<GoToFuns, LinkProps, States> {
readonly currentPath: string
goTo: GoToFuns
Link: React.ComponentClass<LinkProps & { children: any; as?: any; className?: string }>
currentRoute: States
}
export type RouteState<N extends string, OnLoadArgs> = {
name: N
params: Readonly<OnLoadArgs>
}
export type Props<Req extends string, Opt extends string> = Literal<Req> | Literal<Opt>
export type PropTypes<R extends string, Q extends string, PC extends Converters<Props<R, Q>>> = {
[P in Props<R, Q>]: P extends keyof PC ? (PC[P] extends Converter<infer T> ? T : never) : string
}
export type LoadArgs<
R extends string,
Q extends string,
PC extends Converters<Props<R, Q>>,
D
> = PartPartial<PropTypes<R, Q, PC>, Exclude<Literal<Q>, Literal<keyof D>>>
export interface RouterBuilder<T = {}, L = never, S = never> {
start(): RouterStore<T, L, S>
addRoute<
N extends string,
R extends string,
Q extends string,
PC extends Exactly<Converters<Props<R, Q>>, PC>,
D extends Exactly<Partial<PropTypes<R, Q, PC>>, D>,
GTArgs = GoToFunArgs<R, Q, PC, D>,
GTFun = {} extends GTArgs ? Record<N, (arg?: GTArgs) => void> : Record<N, (arg: GTArgs) => void>
>(route: {
name: N
path: [string, R[]] | string
queryParams?: Q[]
onLoad?: (arg: LoadArgs<R, Q, PC, D>) => void
defaults?: D
converters?: PC
component?: ReactComponentCreator<LoadArgs<R, Q, PC, D>>
}): RouterBuilder<
T & GTFun,
L | ({ route: N } & GTArgs),
S | RouteState<N, LoadArgs<R, Q, PC, D>>
>
}
export type GoToFunArgs<
R extends string,
Q extends string,
PC extends Converters<Props<R, Q>>,
D
> = PartPartial<PropTypes<R, Q, PC>, Literal<Q> | Literal<keyof D>>
export type ReactComponentCreator<P> =
| (new (props: P) => React.Component<any>)
| React.StatelessComponent<P>
export type Converters<Params extends string> = Partial<Record<Params, Converter<any>>>
export interface Converter<T> {
toString: (arg: T) => string
fromString: (arg: string) => T
}