From 8f943abc72abb89f35b3cd6d2a57781106cef1b5 Mon Sep 17 00:00:00 2001 From: Alexey Taktarov Date: Thu, 3 Nov 2022 10:24:33 +0100 Subject: [PATCH] Less strict children type in Switch. --- types/ts3.9.4/index.d.ts | 17 ++++----- types/ts3.9.4/type-specs.tsx | 28 +++++++++------ types/ts4.1/index.d.ts | 68 +++++++++++++++--------------------- types/ts4.1/type-specs.tsx | 36 ++++++++++--------- 4 files changed, 73 insertions(+), 76 deletions(-) diff --git a/types/ts3.9.4/index.d.ts b/types/ts3.9.4/index.d.ts index 540046da..cf6e9efd 100644 --- a/types/ts3.9.4/index.d.ts +++ b/types/ts3.9.4/index.d.ts @@ -57,10 +57,9 @@ export type LinkProps = Omit< > & NavigationalProps; -export type RedirectProps = - NavigationalProps & { - children?: never; - }; +export type RedirectProps = NavigationalProps & { + children?: never; +}; export function Redirect( props: PropsWithChildren>, @@ -78,7 +77,7 @@ export function Link( export interface SwitchProps { location?: string; - children: Array>; + children: ReactNode; } export const Switch: FunctionComponent; @@ -103,12 +102,8 @@ export const Router: FunctionComponent< export function useRouter(): RouterProps; -export function useRoute( - pattern: Path -): Match; +export function useRoute(pattern: Path): Match; -export function useLocation< - H extends BaseLocationHook = LocationHook ->(): HookReturnValue; +export function useLocation(): HookReturnValue; // tslint:enable:no-unnecessary-generics diff --git a/types/ts3.9.4/type-specs.tsx b/types/ts3.9.4/type-specs.tsx index 502944dc..0872cd3a 100644 --- a/types/ts3.9.4/type-specs.tsx +++ b/types/ts3.9.4/type-specs.tsx @@ -62,15 +62,11 @@ const invalidParamsWithGeneric: Params<{ id: number }> = { id: 13 }; // $ExpectE This is a mixed content ; - - {(params: Params): React.ReactNode => `User id: ${params.id}`} -; +{(params: Params): React.ReactNode => `User id: ${params.id}`}; path="/users/:id">{({ id }) => `User id: ${id}`}; - path="/users/:id"> - {({ age }) => `User age: ${age}`} // $ExpectError -; + path="/users/:id">{({ age }) => `User age: ${age}`}; // $ExpectError ; // $ExpectError @@ -128,10 +124,7 @@ const invalidParamsWithGeneric: Params<{ id: number }> = { id: 13 }; // $ExpectE Redirect({ href: "/home", delay: 1000 }); // example custom hook -type UseLocWithNoOptions = () => [ - string, - (to: string, foo: number, bar: string) => void -]; +type UseLocWithNoOptions = () => [string, (to: string, foo: number, bar: string) => void]; Redirect({ href: "/app" }); something; // $ExpectError @@ -150,6 +143,21 @@ Redirect({ href: "/app" }); ; + + This won't be rendered, but it's allowed + + <> +
+ I'm a fragment + + {false && Conditionals} + {null} + {undefined} + +; + +; // $ExpectError + /* * Router specs */ diff --git a/types/ts4.1/index.d.ts b/types/ts4.1/index.d.ts index 259943c9..1e3e9a51 100644 --- a/types/ts4.1/index.d.ts +++ b/types/ts4.1/index.d.ts @@ -1,4 +1,5 @@ // Minimum TypeScript Version: 4.1 + // tslint:disable:no-unnecessary-generics import { @@ -19,33 +20,31 @@ import { } from "../use-location"; import { DefaultParams, Params, Match, MatcherFn } from "../matcher"; +import React = require("react"); // 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 - : {}; +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: @@ -60,15 +59,11 @@ export interface RouteProps< RoutePath extends Path = Path > { children?: - | (( - params: T extends DefaultParams ? T : ExtractRouteParams - ) => ReactNode) + | ((params: T extends DefaultParams ? T : ExtractRouteParams) => ReactNode) | ReactNode; path?: RoutePath; component?: ComponentType< - RouteComponentProps< - T extends DefaultParams ? T : ExtractRouteParams - > + RouteComponentProps> >; } @@ -93,10 +88,9 @@ export type LinkProps = Omit< > & NavigationalProps; -export type RedirectProps = - NavigationalProps & { - children?: never; - }; +export type RedirectProps = NavigationalProps & { + children?: never; +}; export function Redirect( props: PropsWithChildren>, @@ -114,7 +108,7 @@ export function Link( export interface SwitchProps { location?: string; - children: Array>; + children: ReactNode; } export const Switch: FunctionComponent; @@ -142,12 +136,8 @@ export function useRouter(): RouterProps; export function useRoute< T extends DefaultParams | undefined = undefined, RoutePath extends Path = Path ->( - pattern: RoutePath -): Match>; +>(pattern: RoutePath): Match>; -export function useLocation< - H extends BaseLocationHook = LocationHook ->(): HookReturnValue; +export function useLocation(): HookReturnValue; // tslint:enable:no-unnecessary-generics diff --git a/types/ts4.1/type-specs.tsx b/types/ts4.1/type-specs.tsx index 2e2dd1ef..dcb6619b 100644 --- a/types/ts4.1/type-specs.tsx +++ b/types/ts4.1/type-specs.tsx @@ -59,19 +59,13 @@ const invalidParamsWithGeneric: Params<{ id: number }> = { id: 13 }; // $ExpectE This is a mixed content ; - - {(params: Params): React.ReactNode => `User id: ${params.id}`} -; +{(params: Params): React.ReactNode => `User id: ${params.id}`}; {({ id }) => `User id: ${id}`}; - - {({ age }) => `User age: ${age}`} // $ExpectError -; +{({ age }) => `User age: ${age}`}; // $ExpectError - - {({ age }: { age: string }) => `User age: ${age}`} -; +{({ age }: { age: string }) => `User age: ${age}`}; ; // $ExpectError @@ -84,9 +78,7 @@ const invalidParamsWithGeneric: Params<{ id: number }> = { id: 13 }; // $ExpectE ; // infer only named params - - {({ first, second }) => `first: ${first}, second: ${second}`} -; +{({ first, second }) => `first: ${first}, second: ${second}`}; // for pathToRegexp matcher @@ -153,10 +145,7 @@ const invalidParamsWithGeneric: Params<{ id: number }> = { id: 13 }; // $ExpectE Redirect({ href: "/home", delay: 1000 }); // example custom hook -type UseLocWithNoOptions = () => [ - string, - (to: string, foo: number, bar: string) => void -]; +type UseLocWithNoOptions = () => [string, (to: string, foo: number, bar: string) => void]; Redirect({ href: "/app" }); something; // $ExpectError @@ -175,6 +164,21 @@ Redirect({ href: "/app" }); ; + + This won't be rendered, but it's allowed + + <> +
+ I'm a fragment + + {false && Conditionals} + {null} + {undefined} + +; + +; // $ExpectError + /* * Router specs */