Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handler return is not type-checked. #1228

Open
canassa opened this issue Jul 26, 2024 · 3 comments
Open

Handler return is not type-checked. #1228

canassa opened this issue Jul 26, 2024 · 3 comments

Comments

@canassa
Copy link
Contributor

canassa commented Jul 26, 2024

When using TypeScript, the return of a handler is not being validated by the type checker.

To Reproduce

Add this file

import type {APIGatewayProxyEvent, APIGatewayProxyResult} from "aws-lambda";
import middy from "@middy/core";


export const handler = middy<APIGatewayProxyEvent, APIGatewayProxyResult>()
    .handler(async (req, context) => {
        return 123
    })
  • Run the type checker.
  • Observe that no error is detected.

Expected behaviour
The type checker should have throw an error since 123 does not match the APIGatewayProxyResult.

Note: I know that this can be workaround by manually adding the return type to the handler function, but that's more error prone.

Environment (please complete the following information):

  • Node.js: 20
  • Middy: 5.4.5
@canassa canassa added the bug label Jul 26, 2024
@canassa
Copy link
Contributor Author

canassa commented Jul 26, 2024

This seems to be a regression with Middy 5. Middy 4 is able to correctly validate the type:

error TS2345: Argument of type '(req: APIGatewayProxyEvent, context: Context) => Promise<number>' is not assignable to parameter of type 'MiddyInputHandler<APIGatewayProxyEvent, APIGatewayProxyResult, Context>'.
  Type 'Promise<number>' is not assignable to type 'void | APIGatewayProxyResult | Promise<APIGatewayProxyResult>'.
    Type 'Promise<number>' is not assignable to type 'Promise<APIGatewayProxyResult>'.
      Type 'number' is not assignable to type 'APIGatewayProxyResult'.

8     .handler(async (req, context) => {
               ~~~~~~~~~~~~~~~~~~~~~~~~~

@willfarrell
Copy link
Member

A PR is welcome for this.

@naorpeled
Copy link
Contributor

naorpeled commented Aug 14, 2024

On it 🙏

Will keep you posted.

jerizm added a commit to jerizm/middy that referenced this issue Sep 18, 2024
jerizm added a commit to jerizm/middy that referenced this issue Sep 18, 2024
jerizm added a commit to jerizm/middy that referenced this issue Sep 18, 2024
jerizm added a commit to jerizm/middy that referenced this issue Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants