#Weird error with Express middleware

38 messages · Page 1 of 1 (latest)

viscid pecan
#

Hey guys,

I have a very odd problem with some code that I have written. It is some Express middleware to be used to make my life easier when working with lots of different API routes etc.

This code does not differ from the previous codebase for this project, but for some reason I am getting errors this time around, and I am honestly clueless as to why.

Here is the error in question:

TS2769: No overload matches this call.
The last overload gave the following error.
Argument of type
(req: Request, res: Response, next: NextFunction) => void | Response<any, Record<string, any>>
is not assignable to parameter of type
RequestHandlerParams<ParamsDictionary, any, any, ParsedQs, Record<string, any>>
Type '(req: Request, res: Response, next: NextFunction) => void | Response<any, Record<string, any>>' is not assignable to type 'RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.
Type void | Response<any, Record<string, any>> is not assignable to type void | Promise<void>
Type Response<any, Record<string, any>> is not assignable to type void | Promise<void>

Now, this error looks pretty self explanatory, however in my middleware code I see no problems:

const User = (allowBanned = false) =>
    (req: Request, res: Response, next: NextFunction) => {
        // Main method logic, uses `return next();` and `return res.json` etc.
    }

Additionally, I am also getting a weird error with Express features that I know damn well exist! Take this code for example:

import { Router } from "express";

const router = Router({
    mergeParams: true, // This would normally not error, but it does..
});

The error in question states that the method expected 0 arguments but got 1. I am genuinely confused, as the Express API has not changed (I even installed the same version as what was used on the last codebase).

I would just really like help if anyone can provide it, thank you in advance.

#

TL;DR - lots of weird TypeScript errors that I haven't had in previous codebases.

#

Here is my tsconfig:

{
    "compilerOptions": {
        "target": "esnext",
        "module": "commonjs",
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "strict": true,
        "moduleResolution": "node",
        "outDir": "dist",
        "experimentalDecorators": true,
        "strictPropertyInitialization": false,
        "strictNullChecks": false,
        "paths": {
            "@/*": ["./src/*"]
        }
    }
}
viscid pecan
#

Additionally one thing I want to mention is that this is a TypeScript error, and I know that because if I add @ts-ignore to the problem lines it compiles fine, which is why I'm posting here

humble fog
#

@viscid pecan Probably the version of @types/express changed between the different projects

viscid pecan
#

I didn't think about that, let me test

viscid pecan
graceful hemlockBOT
#
retsam19#0

Preview:```ts
import {Router} from "express"

const router = Router({
mergeParams: true, // This would normally not error, but it does..
})```

humble fog
#

It's not erroring in the playground which uses the latest version of @types/express so it still seems like it's probably a @types/express issue

#

(Or it's a cached error and if you restart your editor it'd go away)

viscid pecan
#

So how is it that in the playground it doesn't error, but on mine it does, despite having the latest version installed?

#

I have just rolled back to the older version used in a previous codebase

#

(which is what resolved the middleware issue)

#

But this is still showing up

humble fog
#

Probably you rolled back to a versoin before mergeParams was a thing

viscid pecan
#

Definitely not

#

Because I copied the version from my other codebase

#

Which is "@types/express": "^4.17.21",

#

Before I changed it, it was "@types/express": "^5.0.0"

humble fog
#

You may want to constrain those a bit more: every patch version of a types library can have breaking changes

#

Something with ^ can install a pretty wide range of versions.

#

For @types I recommend hardcoding a specific version, but at the least ~ minimizes the blast radius a bit.

viscid pecan
#

I have made that change. Do you know what I can do to fix the mergeParams issue, though?

#

It's weird because you showed the playground that had the latest version, with no error. I had the same version and got the error

#

I have even tried removing package-lock.json and rm -rf node_modules

#

To no avail :/

humble fog
#

I don't really - I guess you can try looking through the typings and see if everything goes where it should

#

And check if you get the issue by running tsc from the command line, or if it's potentially an editor issue

viscid pecan
#

I still get the issue when I run npx tsc

#

I did test that because my thoughts were editor issues, I recently switched from VSCode to WebStorm

#

But it does show up on tsc

humble fog
#

If you peek the typings, you should see something like

#

If you don't see that, I guess that might tell you something

viscid pecan
viscid pecan
#

I am so sorry, I feel like the world's biggest moron, the mergeParams issue was the result of me not importing Router correctly. Thank you for helping me resolve my other issue, will look to migrate to Express 5.0 once I figure out the difference that's causing that error..