[英]Middy with TypeScript, callback type
I'm trying to use TyepScript witn Middy
on a Lambda API call.我正在尝试在 Lambda
Middy
通话中使用 TyepScript 和 Middy。 Part of my code:我的部分代码:
// My types definition
import BodyRequestType from 'types';
// some code
async function myFunction (
event: { body: BodyRequestType },
callback: ??? <--
): Promise<void> {
// my code
return callback(null, {
statusCode: 200,
body: JSON.stringify(data)
});
}
export const handler = middy(myFunction);
I tried to use:我尝试使用:
import { Callback } from 'aws-lambda'
// then...
callback: Callback
But I get this error on middy(myFunction)
:但是我在
middy(myFunction)
上收到这个错误:
TS2345: Argument of type '(event: { body: BodyRequestType; }, callback: Callback<any>) => Promise<void>' is not assignable to
parameter of type 'AsyncHandler<Context>'.
Type '(event: { body: BodyRequestType; }, callback: Callback<any>) => Promise<void>'
is not assignable to type '(event: any, context: Context, callback: Callback<any>) => void'.
Types of parameters 'callback'and 'context' are incompatible.
Type 'Context' is not assignable to type 'Callback<any>'.
Type 'Context' provides no match for the signature '(error?: string | Error | null | undefined, result?: any): void'.
What type should I use on callback
argument of myFunction?我应该在 myFunction 的
callback
参数上使用什么类型?
The problem is the 2nd argument is supposed to be Context
type in its signature instead of the callback.问题是第二个参数在其签名中应该是
Context
类型而不是回调。
Since context is required so you just set context
and callback
as 2nd, 3rd argument respectively as following:由于需要上下文,因此您只需将
context
和callback
分别设置为第二个、第三个参数,如下所示:
import { Context, Callback } from 'aws-lambda';
async function myFunction (
event: { body: BodyRequestType },
context: Context,
callback: Callback
) {
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.