我试图在Lambda调用中使用TyepScript witn Middy。我的代码之一是:
// 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);我试着用:
import { Callback } from 'aws-lambda'
// then...
callback: Callback但是我在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'.我应该在callback参数myFunction中使用哪种类型?
发布于 2020-11-12 03:43:11
问题是第二个参数应该是其签名中的Context类型,而不是回调。
由于需要上下文,所以只需将context和callback分别设置为第2、第3参数,如下所示:
import { Context, Callback } from 'aws-lambda';
async function myFunction (
event: { body: BodyRequestType },
context: Context,
callback: Callback
) {
// ...
}https://stackoverflow.com/questions/64797012
复制相似问题