typescript-is icon indicating copy to clipboard operation
typescript-is copied to clipboard

@AssertType throws synchronously in async methods

Open HitoriSensei opened this issue 3 years ago • 1 comments

When using @AssertType decoator on async methods, calling the method throws synchronously instead of returning rejected promise.

example:

@ValidateClass()
export class MessageHandler {
  async handleMessage(@AssertType() body: { feedId: string }): Promise<void> {
    console.log(body);
  }
}

new MessageHandler()
  .handleMessage({ invalidMessage: 123 }) // error: throws synchronously here!
  .catch(e => {
    // …should be able to handle TypeGuardError here
  })

It would be perfect to make @AssertType aware of async keyword or Promise return type, but is it even possible?

HitoriSensei avatar Jun 30 '21 09:06 HitoriSensei

I've done my research on decorators and metadata and prepared a PR addressing the issue: https://github.com/woutervh-/typescript-is/pull/105

HitoriSensei avatar Jun 30 '21 11:06 HitoriSensei