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 | import * as React from 'react';
|
27 | import * as H from 'history';
|
28 |
|
29 |
|
30 |
|
31 | export interface RouterChildContext<Params extends { [K in keyof Params]?: string } = {}> {
|
32 | router: {
|
33 | history: H.History;
|
34 | route: {
|
35 | location: H.Location;
|
36 | match: match<Params>;
|
37 | };
|
38 | };
|
39 | }
|
40 | export interface MemoryRouterProps {
|
41 | children?: React.ReactNode;
|
42 | initialEntries?: H.LocationDescriptor[] | undefined;
|
43 | initialIndex?: number | undefined;
|
44 | getUserConfirmation?: ((message: string, callback: (ok: boolean) => void) => void) | undefined;
|
45 | keyLength?: number | undefined;
|
46 | }
|
47 |
|
48 | export class MemoryRouter extends React.Component<MemoryRouterProps, any> {}
|
49 |
|
50 | export interface PromptProps {
|
51 | message: string | ((location: H.Location, action: H.Action) => string | boolean);
|
52 | when?: boolean | undefined;
|
53 | }
|
54 | export class Prompt extends React.Component<PromptProps, any> {}
|
55 |
|
56 | export interface RedirectProps {
|
57 | to: H.LocationDescriptor;
|
58 | push?: boolean | undefined;
|
59 | from?: string | undefined;
|
60 | path?: string | undefined;
|
61 | exact?: boolean | undefined;
|
62 | strict?: boolean | undefined;
|
63 | }
|
64 | export class Redirect extends React.Component<RedirectProps, any> {}
|
65 |
|
66 | export interface StaticContext {
|
67 | statusCode?: number | undefined;
|
68 | }
|
69 |
|
70 | export interface RouteComponentProps<
|
71 | Params extends { [K in keyof Params]?: string } = {},
|
72 | C extends StaticContext = StaticContext,
|
73 | S = H.LocationState,
|
74 | > {
|
75 | history: H.History<S>;
|
76 | location: H.Location<S>;
|
77 | match: match<Params>;
|
78 | staticContext?: C | undefined;
|
79 | }
|
80 |
|
81 | export interface RouteChildrenProps<Params extends { [K in keyof Params]?: string } = {}, S = H.LocationState> {
|
82 | history: H.History;
|
83 | location: H.Location<S>;
|
84 | match: match<Params> | null;
|
85 | }
|
86 |
|
87 | export interface RouteProps<
|
88 | Path extends string = string,
|
89 | Params extends { [K: string]: string | undefined } = ExtractRouteParams<Path, string>,
|
90 | > {
|
91 | location?: H.Location | undefined;
|
92 | component?: React.ComponentType<RouteComponentProps<any>> | React.ComponentType<any> | undefined;
|
93 | render?: ((props: RouteComponentProps<Params>) => React.ReactNode) | undefined;
|
94 | children?: ((props: RouteChildrenProps<Params>) => React.ReactNode) | React.ReactNode | undefined;
|
95 | path?: Path | readonly Path[] | undefined;
|
96 | exact?: boolean | undefined;
|
97 | sensitive?: boolean | undefined;
|
98 | strict?: boolean | undefined;
|
99 | }
|
100 | export class Route<T extends {} = {}, Path extends string = string> extends React.Component<
|
101 | RouteProps<Path> & OmitNative<T, keyof RouteProps>,
|
102 | any
|
103 | > {}
|
104 |
|
105 | export interface RouterProps {
|
106 | children?: React.ReactNode;
|
107 | history: H.History;
|
108 | }
|
109 | export class Router extends React.Component<RouterProps, any> {}
|
110 |
|
111 | export interface StaticRouterContext extends StaticContext {
|
112 | url?: string | undefined;
|
113 | action?: 'PUSH' | 'REPLACE' | undefined;
|
114 | location?: object | undefined;
|
115 | }
|
116 | export interface StaticRouterProps {
|
117 | basename?: string | undefined;
|
118 | children?: React.ReactNode;
|
119 | location?: string | object | undefined;
|
120 | context?: StaticRouterContext | undefined;
|
121 | }
|
122 |
|
123 | export class StaticRouter extends React.Component<StaticRouterProps, any> {}
|
124 | export interface SwitchProps {
|
125 | children?: React.ReactNode | undefined;
|
126 | location?: H.Location | undefined;
|
127 | }
|
128 | export class Switch extends React.Component<SwitchProps, any> {}
|
129 |
|
130 | export interface match<Params extends { [K in keyof Params]?: string } = {}> {
|
131 | params: Params;
|
132 | isExact: boolean;
|
133 | path: string;
|
134 | url: string;
|
135 | }
|
136 |
|
137 | // Omit taken from https://github.com/Microsoft/TypeScript/issues/28339#issuecomment-467220238
|
138 | export type Omit<T, K extends keyof T> = T extends any ? Pick<T, Exclude<keyof T, K>> : never;
|
139 |
|
140 | // Newer Omit type: as the previous one is being exported, removing it would be a breaking change
|
141 | export type OmitNative<T, K extends string | number | symbol> = { [P in Exclude<keyof T, K>]: T[P] };
|
142 |
|
143 | export function matchPath<Params extends { [K in keyof Params]?: string }>(
|
144 | pathname: string,
|
145 | props: string | string[] | RouteProps,
|
146 | parent?: match<Params> | null,
|
147 | ): match<Params> | null;
|
148 |
|
149 | export type ExtractRouteOptionalParam<T extends string, U = string | number | boolean> = T extends `${infer Param}?`
|
150 | ? { [k in Param]?: U }
|
151 | : T extends `${infer Param}*`
|
152 | ? { [k in Param]?: U }
|
153 | : T extends `${infer Param}+`
|
154 | ? { [k in Param]: U }
|
155 | : { [k in T]: U };
|
156 |
|
157 | export type ExtractRouteParams<T extends string, U = string | number | boolean> = string extends T
|
158 | ? { [k in string]?: U }
|
159 | : T extends `${infer _Start}:${infer ParamWithOptionalRegExp}/${infer Rest}`
|
160 | ? ParamWithOptionalRegExp extends `${infer Param}(${infer _RegExp})${infer Modifier extends '?' | '+' | '*' | ''}`
|
161 | ? ExtractRouteOptionalParam<`${Param}${Modifier}`, U> & ExtractRouteParams<Rest, U>
|
162 | : ExtractRouteOptionalParam<ParamWithOptionalRegExp, U> & ExtractRouteParams<Rest, U>
|
163 | : T extends `${infer _Start}:${infer ParamWithOptionalRegExp}`
|
164 | ? ParamWithOptionalRegExp extends `${infer Param}(${infer _RegExp})${infer Modifier extends '?' | '+' | '*' | ''}`
|
165 | ? ExtractRouteOptionalParam<`${Param}${Modifier}`, U>
|
166 | : ExtractRouteOptionalParam<ParamWithOptionalRegExp, U>
|
167 | : {};
|
168 |
|
169 | export function generatePath<S extends string>(path: S, params?: ExtractRouteParams<S>): string;
|
170 |
|
171 | export type WithRouterProps<C extends React.ComponentType<any>> = C extends React.ComponentClass
|
172 | ? { wrappedComponentRef?: React.Ref<InstanceType<C>> | undefined }
|
173 | : {};
|
174 |
|
175 | export interface WithRouterStatics<C extends React.ComponentType<any>> {
|
176 | WrappedComponent: C;
|
177 | }
|
178 |
|
179 | // There is a known issue in TypeScript, which doesn't allow decorators to change the signature of the classes
|
180 | // they are decorating. Due to this, if you are using @withRouter decorator in your code,
|
181 | // you will see a bunch of errors from TypeScript. The current workaround is to use withRouter() as a function call
|
182 | // on a separate line instead of as a decorator.
|
183 | export function withRouter<P extends RouteComponentProps<any>, C extends React.ComponentType<P>>(
|
184 | component: C & React.ComponentType<P>,
|
185 | ): React.ComponentClass<Omit<P, keyof RouteComponentProps<any>> & WithRouterProps<C>> & WithRouterStatics<C>;
|
186 |
|
187 | export const __RouterContext: React.Context<RouteComponentProps>;
|
188 |
|
189 | export function useHistory<HistoryLocationState = H.LocationState>(): H.History<HistoryLocationState>;
|
190 |
|
191 | export function useLocation<S = H.LocationState>(): H.Location<S>;
|
192 |
|
193 | export function useParams<Params extends { [K in keyof Params]?: string } = {}>(): Params;
|
194 |
|
195 | export function useRouteMatch<Params extends { [K in keyof Params]?: string } = {}>(): match<Params>;
|
196 | export function useRouteMatch<Params extends { [K in keyof Params]?: string } = {}>(
|
197 | path: string | string[] | RouteProps,
|
198 | ): match<Params> | null;
|
199 |
|
\ | No newline at end of file |