// Type definitions for React Router 5.1 // Project: https://github.com/ReactTraining/react-router // Definitions by: Sergey Buturlakin // Yuichi Murata // Václav Ostrožlík // Nathan Brown // Alex Wendland // Kostya Esmukov // John Reilly // Karol Janyst // Dovydas Navickas // Huy Nguyen // Jérémy Fauvel // Daniel Roth // Egor Shulga // Rahul Raina // Duong Tran // Ben Smith // Wesley Tsai // Sebastian Silbermann // Nicholas Hehr // Pawel Fajfer // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.8 import * as React from 'react'; import * as H from 'history'; // This is the type of the context object that will be passed down to all children of // a `Router` component: export interface RouterChildContext { router: { history: H.History; route: { location: H.Location; match: match; }; }; } export interface MemoryRouterProps { children?: React.ReactNode; initialEntries?: H.LocationDescriptor[] | undefined; initialIndex?: number | undefined; getUserConfirmation?: ((message: string, callback: (ok: boolean) => void) => void) | undefined; keyLength?: number | undefined; } export class MemoryRouter extends React.Component {} export interface PromptProps { message: string | ((location: H.Location, action: H.Action) => string | boolean); when?: boolean | undefined; } export class Prompt extends React.Component {} export interface RedirectProps { to: H.LocationDescriptor; push?: boolean | undefined; from?: string | undefined; path?: string | undefined; exact?: boolean | undefined; strict?: boolean | undefined; } export class Redirect extends React.Component {} export interface StaticContext { statusCode?: number | undefined; } export interface RouteComponentProps< Params extends { [K in keyof Params]?: string } = {}, C extends StaticContext = StaticContext, S = H.LocationState > { history: H.History; location: H.Location; match: match; staticContext?: C | undefined; } export interface RouteChildrenProps { history: H.History; location: H.Location; match: match | null; } export interface RouteProps< Path extends string = string, Params extends { [K: string]: string | undefined } = ExtractRouteParams > { location?: H.Location | undefined; component?: React.ComponentType> | React.ComponentType | undefined; render?: ((props: RouteComponentProps) => React.ReactNode) | undefined; children?: ((props: RouteChildrenProps) => React.ReactNode) | React.ReactNode | undefined; path?: Path | readonly Path[] | undefined; exact?: boolean | undefined; sensitive?: boolean | undefined; strict?: boolean | undefined; } export class Route extends React.Component< RouteProps & OmitNative, any > {} export interface RouterProps { children?: React.ReactNode; history: H.History; } export class Router extends React.Component {} export interface StaticRouterContext extends StaticContext { url?: string | undefined; action?: 'PUSH' | 'REPLACE' | undefined; location?: object | undefined; } export interface StaticRouterProps { basename?: string | undefined; children?: React.ReactNode; location?: string | object | undefined; context?: StaticRouterContext | undefined; } export class StaticRouter extends React.Component {} export interface SwitchProps { children?: React.ReactNode | undefined; location?: H.Location | undefined; } export class Switch extends React.Component {} export interface match { params: Params; isExact: boolean; path: string; url: string; } // Omit taken from https://github.com/Microsoft/TypeScript/issues/28339#issuecomment-467220238 export type Omit = T extends any ? Pick> : never; // Newer Omit type: as the previous one is being exported, removing it would be a breaking change export type OmitNative = { [P in Exclude]: T[P] }; export function matchPath( pathname: string, props: string | string[] | RouteProps, parent?: match | null, ): match | null; export type ExtractRouteOptionalParam = T extends `${infer Param}?` ? { [k in Param]?: U } : T extends `${infer Param}*` ? { [k in Param]?: U } : T extends `${infer Param}+` ? { [k in Param]: U } : { [k in T]: U }; export type ExtractRouteParams = string extends T ? { [k in string]?: U } : T extends `${infer _Start}:${infer ParamWithOptionalRegExp}/${infer Rest}` ? ParamWithOptionalRegExp extends `${infer Param}(${infer _RegExp})` ? ExtractRouteOptionalParam & ExtractRouteParams : ExtractRouteOptionalParam & ExtractRouteParams : T extends `${infer _Start}:${infer ParamWithOptionalRegExp}` ? ParamWithOptionalRegExp extends `${infer Param}(${infer _RegExp})` ? ExtractRouteOptionalParam : ExtractRouteOptionalParam : {}; export function generatePath(path: S, params?: ExtractRouteParams): string; export type WithRouterProps> = C extends React.ComponentClass ? { wrappedComponentRef?: React.Ref> | undefined } : {}; export interface WithRouterStatics> { WrappedComponent: C; } // There is a known issue in TypeScript, which doesn't allow decorators to change the signature of the classes // they are decorating. Due to this, if you are using @withRouter decorator in your code, // you will see a bunch of errors from TypeScript. The current workaround is to use withRouter() as a function call // on a separate line instead of as a decorator. export function withRouter

, C extends React.ComponentType

>( component: C & React.ComponentType

, ): React.ComponentClass> & WithRouterProps> & WithRouterStatics; export const __RouterContext: React.Context; export function useHistory(): H.History; export function useLocation(): H.Location; export function useParams(): Params; export function useRouteMatch(): match; export function useRouteMatch( path: string | string[] | RouteProps, ): match | null;