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