Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/next/src/server/lib/router-utils/typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,13 @@ export function generateValidatorFile(
: type
return `// Validate ${filePath}
{
type __IsExpected<Specific extends ${typeWithRoute}> = Specific
const handler = {} as typeof import(${JSON.stringify(
importPath.replace(/\.tsx?$/, '.js')
)})
handler satisfies ${typeWithRoute}
type __Check = __IsExpected<typeof handler>
// @ts-ignore
type __Unused = __Check
Comment on lines +449 to +455
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we only do this for the versions that don't support satisfies?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gaojude I'm worried that might be brittle. Then we'd have to read a user's package.json and handle monorepo setups correctly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we leave a comment explaining why we're not using satisfies btw

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@feedthejim good point! I'll add that to the PR against canary (#83239)

}`
})
.join('\n\n')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('typed-routes-validator', () => {
it('should generate route validation correctly', async () => {
const dts = await next.readFile('.next/types/validator.ts')
// sanity check that dev generation is working
expect(dts).toContain('handler satisfies AppPageConfig')
expect(dts).toContain('const handler = {} as typeof import(')
})

if (isNextStart) {
Expand Down Expand Up @@ -51,7 +51,7 @@ describe('typed-routes-validator', () => {

expect(exitCode).toBe(1)
expect(cliOutput).toMatch(
/Type error: Type 'typeof import\(.*\)' does not satisfy the expected type 'AppPageConfig<"\/invalid">'/
/Type error: Type 'typeof import\(.*' does not satisfy the constraint 'AppPageConfig</
)
})

Expand Down Expand Up @@ -111,7 +111,7 @@ describe('typed-routes-validator', () => {

expect(exitCode).toBe(1)
expect(cliOutput).toMatch(
/Type error: Type 'typeof import.*does not satisfy the expected type 'RouteHandlerConfig<"\/invalid">'/
/Type error: Type 'typeof import\(.*' does not satisfy the constraint 'RouteHandlerConfig</
)
})

Expand All @@ -132,7 +132,9 @@ describe('typed-routes-validator', () => {
await next.deleteFile('app/invalid-2/route.ts')

expect(exitCode).toBe(1)
expect(cliOutput).toContain(`Types of property 'POST' are incompatible.`)
expect(cliOutput).toMatch(
/Type error: Type 'typeof import\(.*' does not satisfy the constraint 'RouteHandlerConfig</
)
})

it('should pass type checking with valid layout exports', async () => {
Expand Down Expand Up @@ -174,7 +176,7 @@ describe('typed-routes-validator', () => {

expect(exitCode).toBe(1)
expect(cliOutput).toMatch(
/Type error: Type 'typeof import\(.*does not satisfy the expected type 'LayoutConfig<"\/invalid">'/
/Type error: Type 'typeof import\(.*' does not satisfy the constraint 'LayoutConfig</
)
})

Expand Down Expand Up @@ -220,7 +222,7 @@ describe('typed-routes-validator', () => {

expect(exitCode).toBe(1)
expect(cliOutput).toMatch(
/Type error: Type 'typeof import\(.*does not satisfy the expected type 'ApiRouteConfig'/
/Type error: Type 'typeof import\(.*' does not satisfy the constraint 'ApiRouteConfig'/
)
})
}
Expand Down
Loading