Unverified Commit 9df7df13 authored by Balázs Orbán's avatar Balázs Orbán Committed by GitHub

fix(ts): correctly augment `next` when `typedRoutes: true` (#46332)

parent 8d83d85e
...@@ -123,12 +123,11 @@ const edgeRouteTypes: string[] = [] ...@@ -123,12 +123,11 @@ const edgeRouteTypes: string[] = []
export const pageFiles = new Set<string>() export const pageFiles = new Set<string>()
function createRouteDefinitions() { function createRouteDefinitions(originalNextModule: string) {
const fallback = const fallback =
!edgeRouteTypes.length && !nodeRouteTypes.length ? 'string' : '' !edgeRouteTypes.length && !nodeRouteTypes.length ? 'string' : ''
return ` return `type SearchOrHash = \`?\${string}\` | \`#\${string}\`
type SearchOrHash = \`?\${string}\` | \`#\${string}\`
type Suffix = '' | SearchOrHash type Suffix = '' | SearchOrHash
type SafeSlug<S extends string> = type SafeSlug<S extends string> =
...@@ -187,6 +186,7 @@ declare module 'next/link' { ...@@ -187,6 +186,7 @@ declare module 'next/link' {
} }
declare module 'next' { declare module 'next' {
${originalNextModule.replaceAll('../dist', 'next/dist')}
export { Route } export { Route }
}` }`
} }
...@@ -383,6 +383,11 @@ export class NextTypesPlugin { ...@@ -383,6 +383,11 @@ export class NextTypesPlugin {
await Promise.all(promises) await Promise.all(promises)
if (this.typedRoutes) { if (this.typedRoutes) {
const originalNextModule = await fs.readFile(
path.join(__dirname, '../../../../types/index.d.ts'),
'utf-8'
)
pageFiles.forEach((file) => { pageFiles.forEach((file) => {
this.collectPage(file) this.collectPage(file)
}) })
...@@ -391,7 +396,7 @@ export class NextTypesPlugin { ...@@ -391,7 +396,7 @@ export class NextTypesPlugin {
const assetPath = const assetPath =
assetDirRelative + '/' + linkTypePath.replace(/\\/g, '/') assetDirRelative + '/' + linkTypePath.replace(/\\/g, '/')
assets[assetPath] = new sources.RawSource( assets[assetPath] = new sources.RawSource(
createRouteDefinitions() createRouteDefinitions(originalNextModule)
) as unknown as webpack.sources.RawSource ) as unknown as webpack.sources.RawSource
} }
......
import { Metadata } from 'next'
export const meta: Metadata = {
title: 'My App',
}
export default function Root({ children }: { children: React.ReactNode }) { export default function Root({ children }: { children: React.ReactNode }) {
return ( return (
<html> <html>
......
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