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