Unverified Commit b25407e4 authored by Shu Ding's avatar Shu Ding Committed by GitHub

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.
parent ec438418
...@@ -428,12 +428,12 @@ declare module 'next/link' { ...@@ -428,12 +428,12 @@ declare module 'next/link' {
'href' '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. * 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 * @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 export default function Link<RouteType>(props: LinkProps<RouteType>): JSX.Element
......
...@@ -103,7 +103,11 @@ type InternalLinkProps = { ...@@ -103,7 +103,11 @@ type InternalLinkProps = {
// TODO-APP: Include the full set of Anchor props // TODO-APP: Include the full set of Anchor props
// adding this to the publicly exported type currently breaks existing apps // 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 LinkPropsRequired = RequiredKeys<LinkProps>
type LinkPropsOptional = OptionalKeys<InternalLinkProps> type LinkPropsOptional = OptionalKeys<InternalLinkProps>
......
...@@ -47,7 +47,11 @@ export type { ...@@ -47,7 +47,11 @@ export type {
* router.push(returnToPath as Route) * 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 // Extend the React types with missing properties
declare module 'react' { declare module 'react' {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment