Typescript: Error Handling Middleware callback function params are implicitly set to 'any'.
See original GitHub issuePrerequisites
- I have written a descriptive issue title
Mongoose version
6.6.7
Node.js version
18.11.0
MongoDB version
6.0.1
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
12.6
Issue
I’m following this documentation [Error Handling Middleware](https://mongoosejs.com/docs/middleware.html#error-handling-middleware), and TypeScript does not infer the error middleware function arguments if three parameters are given.
userSchema.post('save', function (error, doc, next) { <------ All params implicitly has an 'any' type.
if (error.name === 'MongoServerError' && error.code === 11000) {
next(new Error('There was a duplicate key error'))
} else {
next()
}
})
I know it is possible to set those types explicitly. However, it will lead to a bunch of boilerplate code, which should be inferred from ErrorHandlingMiddlewareFunction
type.
My tsconfig.json:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"rootDir": "src",
"outDir": "dist",
"lib": [
"es6"
],
"removeComments": false,
"strict": true,
"declaration": true,
"declarationDir": "types",
"skipLibCheck": true,
"esModuleInterop": true
},
"include": [
"src",
"test"
],
"exclude": [
"node_modules"
]
}
Issue Analytics
- State:
- Created a year ago
- Comments:6
Top Results From Across the Web
How to simplify parameter types for app.use callback for error ...
It is good practice to have strict typescript. This is how I handle the errors and this are the types for each argument....
Read more >Parameter 'X' implicitly has an 'any' type in TypeScript
The "Parameter 'X' implicitly has an 'any' type" error occurs when a function's parameter has an implicit type of `any`. To solve the...
Read more >A Guide to Error Handling in Express.js | Scout APM Blog
A Guide to Error Handling in Express.js. Error handling often doesn't get the attention and prioritization it deserves.
Read more >Better Error Handling In NodeJS With Error Classes
This article is for JavaScript and NodeJS developers who want to improve error-handling in their applications.
Read more >Usage With TypeScript - Redux Toolkit
The type of the dispatch function type will be directly inferred from the middleware option. So if you add correctly typed middlewares, dispatch ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
See #12723, you’ll have to do the following in 6.8 to help TypeScript out:
It looks like TypeScript struggles to distinguish between
(this: ThisType, res: ResType, next: CallbackWithoutResultAndOptionalError) => void
and(this: ThisType, err: NativeError, res: ResType, next: CallbackWithoutResultAndOptionalError) => void
, maybe because it doesn’t assume that number of arguments can be used to distinguish between 2 function overloads. If I delete the non-error-handlingpost()
definitions, this code compiles fine.We’re probably going to have to add a new feature or way to define error handling middleware that TypeScript can distinguish.