No overload matches this call error ts(2769)
This is my indexWithZod.ts file:
import { z } from 'zod';
export const appWithZod = express();
appWithZod.use(express.json());
const sumInput = z.object({
a: z.number(),
b: z.number()
});
appWithZod.post('/sum', (req, res) => {
const parsedResponse = sumInput.safeParse(req.body);
if (!parsedResponse.success) {
return res.status(411).json({
message: 'Incorrect inputs'
});
}
const answer = parsedResponse.data.a + parsedResponse.data.b;
return res.json({
answer
});
});
I am getting error at (req, res) => { where the exact error message is
No overload matches this call. The last overload gave the following error. Argument of type '(req: Request<{}, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>, number>) => Response<any, Record<...>, number>' is not assignable to parameter of type 'Application<Record<string, any>>'. Type '(req: Request<{}, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>, number>) => Response<any, Record<...>, number>' is missing the following properties from type 'Application<Record<string, any>>': init, defaultConfiguration, engine, set, and 63 more.ts(2769)
Here is the additional bin.ts file for reference:
import { appWithZod } from './indexWithZod';
appWithZod.listen(3001, () => {
console.log('Server (appWithZod) is running on PORT 3001');
});
Your problem lies at: return res.status(411).json({ message: 'Incorrect inputs' }); and return res.json({ answer });.
Removing the return statements should fix your problem, the TS error clarifies that you passed a (req, res) => Response type instead of an Application type. You can just call res.status and json then return to exit the function.
This should be fixed with the latest version of the express-server-static types.