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

Closed
canassa opened this issue Jul 26, 2024 · 3 comments · Fixed by #1244
Closed

Handler return is not type-checked. #1228

canassa opened this issue Jul 26, 2024 · 3 comments · Fixed by #1244

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
canassa pushed a commit to canassa/middy that referenced this issue Oct 15, 2024
- This change addresses an issue where the return type of handlers was not being correctly enforced by the TypeScript compiler. As far as I could tell, the issue is caused by the `infer` keyword not working with generic types.
- Added `TResult` as a type parameter to `MiddlewareHandler` to explicitly specify the handler's return type.
- Modified the `middy` function signature to pass `TResult` to `MiddlewareHandler`.
- By explicitly specifying `TResult`, we ensure that the compiler can correctly enforce the handler's return type.

closes middyjs#1228
canassa pushed a commit to canassa/middy that referenced this issue Oct 15, 2024
- This change addresses an issue where the return type of handlers was not being correctly enforced by the TypeScript compiler. As far as I could tell, the issue is caused by the `infer` keyword not working with generic types.
- Added `TResult` as a type parameter to `MiddlewareHandler` to explicitly specify the handler's return type.
- Modified the `middy` function signature to pass `TResult` to `MiddlewareHandler`.
- By explicitly specifying `TResult`, we ensure that the compiler can correctly enforce the handler's return type.

closes middyjs#1228
canassa pushed a commit to canassa/middy that referenced this issue Oct 18, 2024
- This change addresses an issue where the return type of handlers was not being correctly enforced by the TypeScript compiler. As far as I could tell, the issue is caused by the `infer` keyword not working with generic types.
- Added `TResult` as a type parameter to `MiddlewareHandler` to explicitly specify the handler's return type.
- Modified the `middy` function signature to pass `TResult` to `MiddlewareHandler`.
- By explicitly specifying `TResult`, we ensure that the compiler can correctly enforce the handler's return type.

closes middyjs#1228
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

3 participants