UNPKG

8.26 kBTypeScriptView Raw
1// Type definitions for React Router 5.1
2// Project: https://github.com/ReactTraining/react-router
3// Definitions by: Sergey Buturlakin <https://github.com/sergey-buturlakin>
4// Yuichi Murata <https://github.com/mrk21>
5// Václav Ostrožlík <https://github.com/vasek17>
6// Nathan Brown <https://github.com/ngbrown>
7// Alex Wendland <https://github.com/awendland>
8// Kostya Esmukov <https://github.com/KostyaEsmukov>
9// John Reilly <https://github.com/johnnyreilly>
10// Karol Janyst <https://github.com/LKay>
11// Dovydas Navickas <https://github.com/DovydasNavickas>
12// Huy Nguyen <https://github.com/huy-nguyen>
13// Jérémy Fauvel <https://github.com/grmiade>
14// Daniel Roth <https://github.com/DaIgeb>
15// Egor Shulga <https://github.com/egorshulga>
16// Rahul Raina <https://github.com/rraina>
17// Duong Tran <https://github.com/t49tran>
18// Ben Smith <https://github.com/8enSmith>
19// Wesley Tsai <https://github.com/wezleytsai>
20// Sebastian Silbermann <https://github.com/eps1lon>
21// Nicholas Hehr <https://github.com/HipsterBrown>
22// Pawel Fajfer <https://github.com/pawfa>
23// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
24// TypeScript Version: 2.8
25
26import * as React from 'react';
27import * as H from 'history';
28
29// This is the type of the context object that will be passed down to all children of
30// a `Router` component:
31export 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}
40export 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
48export class MemoryRouter extends React.Component<MemoryRouterProps, any> {}
49
50export interface PromptProps {
51 message: string | ((location: H.Location, action: H.Action) => string | boolean);
52 when?: boolean | undefined;
53}
54export class Prompt extends React.Component<PromptProps, any> {}
55
56export 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}
64export class Redirect extends React.Component<RedirectProps, any> {}
65
66export interface StaticContext {
67 statusCode?: number | undefined;
68}
69
70export 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
81export 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
87export 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}
100export class Route<T extends {} = {}, Path extends string = string> extends React.Component<
101 RouteProps<Path> & OmitNative<T, keyof RouteProps>,
102 any
103> {}
104
105export interface RouterProps {
106 children?: React.ReactNode;
107 history: H.History;
108}
109export class Router extends React.Component<RouterProps, any> {}
110
111export interface StaticRouterContext extends StaticContext {
112 url?: string | undefined;
113 action?: 'PUSH' | 'REPLACE' | undefined;
114 location?: object | undefined;
115}
116export interface StaticRouterProps {
117 basename?: string | undefined;
118 children?: React.ReactNode;
119 location?: string | object | undefined;
120 context?: StaticRouterContext | undefined;
121}
122
123export class StaticRouter extends React.Component<StaticRouterProps, any> {}
124export interface SwitchProps {
125 children?: React.ReactNode | undefined;
126 location?: H.Location | undefined;
127}
128export class Switch extends React.Component<SwitchProps, any> {}
129
130export 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
138export 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
141export type OmitNative<T, K extends string | number | symbol> = { [P in Exclude<keyof T, K>]: T[P] };
142
143export 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
149export 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
157export 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})`
161 ? ExtractRouteOptionalParam<Param, 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})`
165 ? ExtractRouteOptionalParam<Param, U>
166 : ExtractRouteOptionalParam<ParamWithOptionalRegExp, U>
167 : {};
168
169export function generatePath<S extends string>(path: S, params?: ExtractRouteParams<S>): string;
170
171export type WithRouterProps<C extends React.ComponentType<any>> = C extends React.ComponentClass
172 ? { wrappedComponentRef?: React.Ref<InstanceType<C>> | undefined }
173 : {};
174
175export 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.
183export 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
187export const __RouterContext: React.Context<RouteComponentProps>;
188
189export function useHistory<HistoryLocationState = H.LocationState>(): H.History<HistoryLocationState>;
190
191export function useLocation<S = H.LocationState>(): H.Location<S>;
192
193export function useParams<Params extends { [K in keyof Params]?: string } = {}>(): Params;
194
195export function useRouteMatch<Params extends { [K in keyof Params]?: string } = {}>(): match<Params>;
196export 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