import type { FC, ReactNode } from 'react'; type Path = string; type MaybePath = Path | undefined; export type Params = Record; // CREDIT @types/react-router // https://github.com/DefinitelyTyped/DefinitelyTyped/blob/3067ea199822cc2f06edcb84854adeecdfe640ad/types/react-router/index.d.ts#L149 export type ExtractRouteOptionalParam = T extends `${infer Param}?` ? { [k in Param]?: string } : T extends `${infer Param}*` ? { [k in Param]?: string } : T extends `${infer Param}+` ? { [k in Param]: string } : { [k in T]: string }; // CREDIT @types/react-router // https://github.com/DefinitelyTyped/DefinitelyTyped/blob/3067ea199822cc2f06edcb84854adeecdfe640ad/types/react-router/index.d.ts#L149 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 : {}; export interface RoutingProps { path: TPath; wildcard?: boolean | undefined; } export type RouteComponentProps = | { children: ReactNode; // path?: never; // component?: never; } | { component: FC>; // path?: never; // children?: never; } | (RoutingProps & { children: ReactNode; // component?: never; }) | (RoutingProps & { component: FC>; // children?: never; }); export type PartialWithUndefined = { [P in keyof T]?: T[P] | undefined; }; export interface URLProps { hash: string; host: string; hostname: string; // href: string; origin: string; password: string; pathname: string; port: string; protocol: string; search: string; searchParams: URLSearchParams; username: string; } // these are the only things that can change with history API export interface RestrictedURLProps { hash: string; pathname: string; searchParams: URLSearchParams; // things you can never provide (just to make sure TS doesnt allow // a stray URL object to be passed) origin?: never; username?: never; password?: never; hostname?: never; host?: never; port?: never; } export type RequireKeys = Required< Pick > & Omit;