How add type to body of the route handler request ?
Unanswered
Southeastern blueberry bee posted this in #help-forum
Southeastern blueberry beeOP
I tried this
The types of the 'body' property are incompatible.
Type '{ email: string; password: string; }' does not have the following properties of type 'ReadableStream<Uint8Array>': locked, cancel, getReader, pipeThrough and 2 others.ts(2430)"
interface ExtendedRequest extends Request {
body: {
email: string;
password: string;
};
} but im getting this "The 'ExtendedRequest' interface incorrectly extends the 'Request' interface.The types of the 'body' property are incompatible.
Type '{ email: string; password: string; }' does not have the following properties of type 'ReadableStream<Uint8Array>': locked, cancel, getReader, pipeThrough and 2 others.ts(2430)"
2 Replies
How do you read the body? Normally you can add types to the parsed JSON body.
You can combine it with validators like [zod](https://zod.dev), to make sure the request body is valid:
By default,
const body = await req.json() as Types;You can combine it with validators like [zod](https://zod.dev), to make sure the request body is valid:
const body = mySchema.parse(await req.json())By default,
req.body is a readable stream (not a JSON object)Be careful that Next.js won't validate the body for you, when calling
req.json, the result might not match with your type bindings