I have just migrated from NextJS 14 to 15 using the app router method. On Windows.
I am having an issue with one of my static routes. It is not dynamic route, but is gettings errors as if it was.
interface Context {
data: object;
params: any;
}
export async function POST(request: NextRequest, context: Context) {
const testBody = await request.json();
....function continues on....
......
When I run: npm run dev
it works fine, but when I build with : npm run build
it fails to pass linting and checking validity of types
. And I get the following error:
PS C:\Users\SJ\Desktop\nextjsProject> npm run build
> build
> next build
▲ Next.js 15.0.3
- Environments: .env
Creating an optimized production build ...
✓ Compiled successfully
Linting and checking validity of types ..Failed to compile.
.next/types/app/api/myRoute/route.ts:166:7
Type error: Type '{ __tag__: "POST"; __param_position__: "second"; __param_type__: Context; }' does not satisfy the constraint 'ParamCheck<RouteContext>'.
Types of property '__param_type__' are incompatible.
Property 'params' is missing in type 'Context' but required in type 'RouteContext'.
164 | Diff<
165 | ParamCheck<aRouteContext>,
> 166 | {
| ^
167 | __tag__: 'POST'
168 | __param_position__: 'second'
169 | __param_type__: SecondArg<MaybeField<TEntry, 'POST'>>
Interface Context
params to a type of Promise<string>
and Promise<any>
Interface Context
to type Context
context : Context
I pass in context: {params: Promise<any>}
Please help!
Replace context: Context
with { params }: { params: Promise<{ id: string }> }
Then, retrieve the value using:
const id = (await params).id