davidbanham/express-async-errors

support typescript?

imbooo opened this issue · 4 comments

support typescript?

Can you provide a little more detail here? In what way does Typescript not work with the package as it currently exists?

And how exactly would you type check that thing:

// you can return promises...
app.use((req, res, next) => {
  return Promise.reject(new Error('Ouch'));
});

// ... or use async functions (that return promises)
app.use(async (req, res, next) => {
  throw new Error('Ouch');
});

// ... but you don't have to
app.use((req, res, next) => {
  doSomeStuff();
  next();
});
app.use((req, res, next) => {
  res.send('I\'m done with examples :D');
});

You might say:
ok, it can be any function or function that returns possibly rejected promise but that already falls under any category...
Here is express's handler definition: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/2372530/types/express-serve-static-core/index.d.ts#L33-L36

RequestHandler (and RequestParamHandler) can be literally anything and, as @davidbanham already said, I don't see how this library puts any additional constraints on top of that 🙃

This lib works just fine for me with TypeScript 👍

Just a thought...this request might just be because people are trying to use an import statement instead of require()? Import kicks out a Could not find a declaration file for module 'express-async-errors'. error. So there are two options:

  1. Add a declaration (.d.ts) file containing declare module 'express-async-errors';
  2. Just use require('express-async-errors') (might need to disable linter for that line)

Seems obvious enough, but it might make sense to add this to the README just to help people along 🤷‍♂

I created a express-async-errors.d.ts with the following content:

declare module 'express-async-errors'

And when I try to import it, it just gets ignored (I keep getting an UnhandledPromiseRejectionWarning)

I tried the following variations:

// import expressAsyncErrors from 'express-async-errors'
// import * as expressAsyncErrors from 'express-async-errors'
// import { default as expressAsyncErrors } from 'express-async-errors'
require('express-async-errors')

The uncommented require works fine.

And I do agree that a simple paragraph explaining the correct way to use it with TypeScript would be very valuable, no matter how obvious it might seem to be.