diff --git a/packages/core/index.d.ts b/packages/core/index.d.ts index cf0a0c78b..375890e5c 100644 --- a/packages/core/index.d.ts +++ b/packages/core/index.d.ts @@ -99,7 +99,7 @@ export interface MiddyfiedHandler< handler: ( handler: MiddlewareHandler< LambdaHandler, - TContext + TContext, TResult > ) => MiddyfiedHandler } @@ -150,8 +150,9 @@ infer TMiddlewareInternal declare type MiddlewareHandler< THandler extends LambdaHandler, - TContext extends LambdaContext = LambdaContext -> = THandler extends LambdaHandler // always true + TContext extends LambdaContext = LambdaContext, + TResult = any +> = THandler extends LambdaHandler // always true ? MiddyInputHandler : never @@ -169,7 +170,7 @@ declare function middy< > ( handler?: | LambdaHandler - | MiddlewareHandler, TContext> + | MiddlewareHandler, TContext, TResult> | PluginObject, plugin?: PluginObject ): MiddyfiedHandler diff --git a/packages/core/index.test-d.ts b/packages/core/index.test-d.ts index bec0f5c09..2680f7f28 100644 --- a/packages/core/index.test-d.ts +++ b/packages/core/index.test-d.ts @@ -408,4 +408,19 @@ const s3Handler = async (event: S3Event): Promise => { } const handler1182 = middy().handler(s3Handler) -expectType>(handler1182) +expectType>(handler1182) + +// Issue #1228 Correct return type +const numberHandler = middy() + .handler(async (event) => { + return 42 // Correct return type, should pass type checking + }) +expectType>(numberHandler) + +// Issue #1228 Incorrect return type +const invalidNumberHandler = middy() + // @ts-expect-error + .handler(async () => { + return 'not a number' + }) +expectType>(invalidNumberHandler)