forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
types-plugin: enahnce type checking on interface params (vercel#71059)
### What This PR enhances the next-types-plugin to generate more compatible types for cases like typed `params` with a `interface` type `page.tsx` or `layout.tsx` #### Issue Previously users might break with type check ```tsx interface Params { slug: string } // this will fail export default async function Page({ params }: { params: Promise<Params>}) ``` To workaround users have to turn it into a more general `Record` like type ```diff interface Params { slug: string + [key: string]: string } ``` This PR helps get rid of the above workaround. You just need to type it properly with Promise for `params` then it's all done.
- Loading branch information
Showing
5 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
test/production/app-dir/next-types-plugin/sync-params-type-check/app/blog/[slug]/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
interface Params { | ||
slug: string | ||
} | ||
|
||
export default async function Page({ params }: { params: Promise<Params> }) { | ||
const paramsValue = await params | ||
return <p>slug:{paramsValue.slug}</p> | ||
} |
8 changes: 8 additions & 0 deletions
8
test/production/app-dir/next-types-plugin/sync-params-type-check/app/layout.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ReactNode } from 'react' | ||
export default function Root({ children }: { children: ReactNode }) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
3 changes: 3 additions & 0 deletions
3
test/production/app-dir/next-types-plugin/sync-params-type-check/app/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Page() { | ||
return <p>hello world</p> | ||
} |
35 changes: 35 additions & 0 deletions
35
...roduction/app-dir/next-types-plugin/sync-params-type-check/sync-params-type-check.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
|
||
// This next-types-plugin feature only works in webpack | ||
;(process.env.TURBOPACK ? describe.skip : describe)( | ||
'app-dir - sync-params-type-check', | ||
() => { | ||
const { next } = nextTestSetup({ | ||
files: __dirname, | ||
skipStart: true, | ||
skipDeployment: true, | ||
}) | ||
|
||
it('should pass build with Promise params', async () => { | ||
const { exitCode } = await next.build() | ||
expect(exitCode).toBe(0) | ||
}) | ||
|
||
it('should fail build with sync params', async () => { | ||
await next.patchFile( | ||
'app/blog/[slug]/page.tsx', | ||
` | ||
interface Params { slug: string } | ||
export default function Page({ params }: { params: Params }) { | ||
return <p>slug:{params.slug}</p> | ||
} | ||
` | ||
) | ||
const { exitCode, cliOutput } = await next.build() | ||
expect(exitCode).toBe(1) | ||
expect(cliOutput).toMatch( | ||
/Type error: Type '{ params: Params; }' does not satisfy the constraint 'PageProps'/ | ||
) | ||
}) | ||
} | ||
) |