UNPKG

6.79 kBTypeScriptView Raw
1import * as React from 'react';
2import * as H from 'history';
3
4// This is the type of the context object that will be passed down to all children of
5// a `Router` component:
6export 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}
15export 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
23export class MemoryRouter extends React.Component<MemoryRouterProps, any> {}
24
25export interface PromptProps {
26 message: string | ((location: H.Location, action: H.Action) => string | boolean);
27 when?: boolean | undefined;
28}
29export class Prompt extends React.Component<PromptProps, any> {}
30
31export 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}
39export class Redirect extends React.Component<RedirectProps, any> {}
40
41export interface StaticContext {
42 statusCode?: number | undefined;
43}
44
45export 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
56export 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
62export 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}
75export class Route<T extends {} = {}, Path extends string = string> extends React.Component<
76 RouteProps<Path> & OmitNative<T, keyof RouteProps>,
77 any
78> {}
79
80export interface RouterProps {
81 children?: React.ReactNode;
82 history: H.History;
83}
84export class Router extends React.Component<RouterProps, any> {}
85
86export interface StaticRouterContext extends StaticContext {
87 url?: string | undefined;
88 action?: 'PUSH' | 'REPLACE' | undefined;
89 location?: object | undefined;
90}
91export interface StaticRouterProps {
92 basename?: string | undefined;
93 children?: React.ReactNode;
94 location?: string | object | undefined;
95 context?: StaticRouterContext | undefined;
96}
97
98export class StaticRouter extends React.Component<StaticRouterProps, any> {}
99export interface SwitchProps {
100 children?: React.ReactNode | undefined;
101 location?: H.Location | undefined;
102}
103export class Switch extends React.Component<SwitchProps, any> {}
104
105export 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
113export 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
116export type OmitNative<T, K extends string | number | symbol> = { [P in Exclude<keyof T, K>]: T[P] };
117
118export 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
124export 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
132export 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
144export function generatePath<S extends string>(path: S, params?: ExtractRouteParams<S>): string;
145
146export type WithRouterProps<C extends React.ComponentType<any>> = C extends React.ComponentClass
147 ? { wrappedComponentRef?: React.Ref<InstanceType<C>> | undefined }
148 : {};
149
150export 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.
158export 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
162export const __RouterContext: React.Context<RouteComponentProps>;
163
164export function useHistory<HistoryLocationState = H.LocationState>(): H.History<HistoryLocationState>;
165
166export function useLocation<S = H.LocationState>(): H.Location<S>;
167
168export function useParams<Params extends { [K in keyof Params]?: string } = {}>(): Params;
169
170export function useRouteMatch<Params extends { [K in keyof Params]?: string } = {}>(): match<Params>;
171export 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