UNPKG

8.16 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 initialEntries?: H.LocationDescriptor[] | undefined;
42 initialIndex?: number | undefined;
43 getUserConfirmation?: ((message: string, callback: (ok: boolean) => void) => void) | undefined;
44 keyLength?: number | undefined;
45}
46
47export class MemoryRouter extends React.Component<MemoryRouterProps, any> {}
48
49export interface PromptProps {
50 message: string | ((location: H.Location, action: H.Action) => string | boolean);
51 when?: boolean | undefined;
52}
53export class Prompt extends React.Component<PromptProps, any> {}
54
55export 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}
63export class Redirect extends React.Component<RedirectProps, any> {}
64
65export interface StaticContext {
66 statusCode?: number | undefined;
67}
68
69export 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
80export 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
86export 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}
99export class Route<T extends {} = {}, Path extends string = string> extends React.Component<
100 RouteProps<Path> & OmitNative<T, keyof RouteProps>,
101 any
102> {}
103
104export interface RouterProps {
105 history: H.History;
106}
107export class Router extends React.Component<RouterProps, any> {}
108
109export interface StaticRouterContext extends StaticContext {
110 url?: string | undefined;
111 action?: 'PUSH' | 'REPLACE' | undefined;
112 location?: object | undefined;
113}
114export interface StaticRouterProps {
115 basename?: string | undefined;
116 location?: string | object | undefined;
117 context?: StaticRouterContext | undefined;
118}
119
120export class StaticRouter extends React.Component<StaticRouterProps, any> {}
121export interface SwitchProps {
122 children?: React.ReactNode | undefined;
123 location?: H.Location | undefined;
124}
125export class Switch extends React.Component<SwitchProps, any> {}
126
127export 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
135export 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
138export type OmitNative<T, K extends string | number | symbol> = { [P in Exclude<keyof T, K>]: T[P] };
139
140export 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
146export 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
154export 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
166export function generatePath<S extends string>(path: S, params?: ExtractRouteParams<S>): string;
167
168export type WithRouterProps<C extends React.ComponentType<any>> = C extends React.ComponentClass
169 ? { wrappedComponentRef?: React.Ref<InstanceType<C>> | undefined }
170 : {};
171
172export 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.
180export 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
184export const __RouterContext: React.Context<RouteComponentProps>;
185
186export function useHistory<HistoryLocationState = H.LocationState>(): H.History<HistoryLocationState>;
187
188export function useLocation<S = H.LocationState>(): H.Location<S>;
189
190export function useParams<Params extends { [K in keyof Params]?: string } = {}>(): Params;
191
192export function useRouteMatch<Params extends { [K in keyof Params]?: string } = {}>(): match<Params>;
193export 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