// Minimum TypeScript Version: 4.1 // tslint:disable:no-unnecessary-generics import { AnchorHTMLAttributes, FunctionComponent, PropsWithChildren, ComponentType, ReactElement, ReactNode, } from "react"; import { Path, BaseLocationHook, HookReturnValue, HookNavigationOptions, LocationHook, } from "../use-location"; import { DefaultParams, Params, Match, MatcherFn } from "../matcher"; // re-export types from these modules export * from "../matcher"; export * from "../use-location"; export type ExtractRouteOptionalParam = PathType extends `${infer Param}?` ? { [k in Param]: string | undefined } : PathType extends `${infer Param}*` ? { [k in Param]: string | undefined } : PathType extends `${infer Param}+` ? { [k in Param]: string } : { [k in PathType]: string }; export type ExtractRouteParams = string extends PathType ? { [k in string]: string } : PathType extends `${infer _Start}:${infer ParamWithOptionalRegExp}/${infer Rest}` ? ParamWithOptionalRegExp extends `${infer Param}(${infer _RegExp})` ? ExtractRouteOptionalParam & ExtractRouteParams : ExtractRouteOptionalParam & ExtractRouteParams : PathType extends `${infer _Start}:${infer ParamWithOptionalRegExp}` ? ParamWithOptionalRegExp extends `${infer Param}(${infer _RegExp})` ? ExtractRouteOptionalParam : ExtractRouteOptionalParam : {}; /* * Components: */ export interface RouteComponentProps { params: T; } export interface RouteProps< T extends DefaultParams | undefined = undefined, RoutePath extends Path = Path > { children?: | (( params: T extends DefaultParams ? T : ExtractRouteParams ) => ReactNode) | ReactNode; path?: RoutePath; component?: ComponentType< RouteComponentProps< T extends DefaultParams ? T : ExtractRouteParams > >; } export function Route< T extends DefaultParams | undefined = undefined, RoutePath extends Path = Path >(props: RouteProps): ReactElement | null; /* * Components: & */ export type NavigationalProps = ( | { to: Path; href?: never } | { href: Path; to?: never } ) & HookNavigationOptions; export type LinkProps = Omit< AnchorHTMLAttributes, "href" > & NavigationalProps; export type RedirectProps = NavigationalProps & { children?: never; }; export function Redirect( props: PropsWithChildren>, context?: any ): ReactElement | null; export function Link( props: PropsWithChildren>, context?: any ): ReactElement | null; /* * Components: */ export interface SwitchProps { location?: string; children: Array>; } export const Switch: FunctionComponent; /* * Components: */ export interface RouterProps { hook: BaseLocationHook; base: Path; matcher: MatcherFn; } export const Router: FunctionComponent< Partial & { children: ReactNode; } >; /* * Hooks */ export function useRouter(): RouterProps; export function useRoute< T extends DefaultParams | undefined = undefined, RoutePath extends Path = Path >( pattern: RoutePath ): Match>; export function useLocation< H extends BaseLocationHook = LocationHook >(): HookReturnValue; // tslint:enable:no-unnecessary-generics