davidbanham / express-async-errors

async/await support for ExpressJS
Other
891 stars 42 forks source link

support typescript? #18

Closed imbooo closed 4 years ago

davidbanham commented 5 years ago

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

vladimyr commented 5 years ago

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 :upside_down_face:

djdmbrwsk commented 4 years ago

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 🤷‍♂

opensas commented 4 years ago

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.