middyjs / middy

🛵 The stylish Node.js middleware engine for AWS Lambda 🛵

Home Page:https://middy.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Typescript error in handler parameters

kirbyjs opened this issue · comments

Describe the bug
The main Typescript compile error occurs when trying to accept { signal } parameter (https://middy.js.org/docs/writing-middlewares/timeouts) in the handler. The error is Types of parameters  abortControllerConfig  and  callback  are incompatible.

To Reproduce
How to reproduce the behavior:

  1. Try to add the { signal } 3rd parameter to the handler in a typescript repo
  2. Typescript compiler error should occur

Expected behavior
No typescript compile errors when trying to accept the signal parameter

Environment (please complete the following information):

  • Node.js: 18
  • Middy: 4.6.4
  • AWS SDK 3

Additional context

Thanks for reporting. Apologies on the delay. Can you put a full example in the issue?

It might be as simple as a one line fix here https://github.com/middyjs/middy/blob/main/packages/core/index.d.ts#L71, if I'm understanding correctly. A PR is also welcome.

@willfarrell I stumbled on this issue as well.

Property 'signal' does not exist on type 'Callback<...>'.

The line of code you highlighted is exactly where the issue exists. Middy is passing { signal } to the handler but the typescript MiddyInputHandler type expects a callback function.

It's perhaps as simple as changing the MiddyInputHandler type to:

type MiddyInputHandler<
  TEvent,
  TResult,
  TContext extends LambdaContext = LambdaContext
> = (
  event: TEvent,
  context: TContext,
  params: { signal: AbortSignal } // <--- an object instead of a callback

But I'm not familiar enough with Middy to know if this will break something else.

not familiar enough with Middy to know if this will break something else.

It shouldn't break anything else. Did you want to make the PR?

Closed with PR #1165