Add Route
and LinkProps
stub generics (#54226)
Closes #53732. Closes #52929. When using the statically typed routes feature, we might have code like: ```ts export function Card<T extends string>({ href }: { href: Route<T> | URL })... export function Card<T extends string>({ href }: LinkProps<T>)... ``` To statically check `<Card href={...}>` and make sure it's `href` is an existing route. However, in certain cases these route types are not generated (e.g. running `tsc` directly w/o a `next dev` or `next build`), which results in TS errors. This PR adds stub generics to `Route` and `LinkProps` so even if that plugin isn't executed, these types will not block type checking.
This commit is contained in:
parent
ec438418a4
commit
b25407e4e4
3 changed files with 12 additions and 4 deletions
|
@ -428,12 +428,12 @@ declare module 'next/link' {
|
|||
'href'
|
||||
>
|
||||
|
||||
export type LinkProps<T> = LinkRestProps & {
|
||||
export type LinkProps<RouteInferType> = LinkRestProps & {
|
||||
/**
|
||||
* The path or URL to navigate to. This is the only required prop. It can also be an object.
|
||||
* @see https://nextjs.org/docs/api-reference/next/link
|
||||
*/
|
||||
href: __next_route_internal_types__.RouteImpl<T> | UrlObject
|
||||
href: __next_route_internal_types__.RouteImpl<RouteInferType> | UrlObject
|
||||
}
|
||||
|
||||
export default function Link<RouteType>(props: LinkProps<RouteType>): JSX.Element
|
||||
|
|
|
@ -103,7 +103,11 @@ type InternalLinkProps = {
|
|||
|
||||
// TODO-APP: Include the full set of Anchor props
|
||||
// adding this to the publicly exported type currently breaks existing apps
|
||||
export type LinkProps = InternalLinkProps
|
||||
|
||||
// `RouteInferType` is a stub here to avoid breaking `typedRoutes` when the type
|
||||
// isn't generated yet. It will be replaced when the webpack plugin runs.
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
export type LinkProps<RouteInferType = any> = InternalLinkProps
|
||||
type LinkPropsRequired = RequiredKeys<LinkProps>
|
||||
type LinkPropsOptional = OptionalKeys<InternalLinkProps>
|
||||
|
||||
|
|
6
packages/next/types/index.d.ts
vendored
6
packages/next/types/index.d.ts
vendored
|
@ -47,7 +47,11 @@ export type {
|
|||
* router.push(returnToPath as Route)
|
||||
* ```
|
||||
*/
|
||||
export type Route = string & {}
|
||||
|
||||
// `RouteInferType` is a stub here to avoid breaking `typedRoutes` when the type
|
||||
// isn't generated yet. It will be replaced when the webpack plugin runs.
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
export type Route<RouteInferType = any> = string & {}
|
||||
|
||||
// Extend the React types with missing properties
|
||||
declare module 'react' {
|
||||
|
|
Loading…
Reference in a new issue