question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript: Error Handling Middleware callback function params are implicitly set to 'any'.

See original GitHub issue

Prerequisites

  • 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:closed
  • Created a year ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
vkarpov15commented, Dec 4, 2022

See #12723, you’ll have to do the following in 6.8 to help TypeScript out:

userSchema.post('save', { errorHandler: true }, function (error, doc, next) {
  if (error.name === 'MongoServerError' && error.code === 11000) {
    next(new Error('There was a duplicate key error'))
  } else {
    next()
  }
})
1reaction
vkarpov15commented, Nov 23, 2022

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-handling post() 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found