为什么 Typescript 显示已弃用 Lambda 处理程序的事件?

Why does Typescript shows event deprecated for Lambda Handler?

我正在尝试在打字稿环境中定义 lambda 处理程序。

const sampleFunc = async (event) => {
  console.log('request:', JSON.stringify(event, undefined, 2));
  return {
    statusCode: 200,
    headers: { 'Content-Type': 'text/plain' },
    body: `Hello, CDK! You've hit ${event.path}\n`,
  };
};
exports.handler = sampleFunc(event);

对于事件它是删除线(无法在问题中格式化)并且编译器说它已被弃用。

弃用消息:

'event' is deprecated.ts(6385)
lib.dom.d.ts(17314, 5): The declaration was marked as deprecated here.

然而,当我没有单独定义函数时,对于相同的代码,它是有效的。

    exports.handler = async function (event) {
  console.log('request:', JSON.stringify(event, undefined, 2));
  return {
    statusCode: 200,
    headers: { 'Content-Type': 'text/plain' },
    body: `Hello, CDK! You've hit ${event.path}\n`,
  };
};

我认为错误不在于函数定义本身,而在于导出它的方式。

您必须导出 函数 而不是 调用 它:

错误:

exports.handler = sampleFunc(event);

右:

exports.handler = sampleFunc;

您也可以直接导出函数:

exports.handler = async (event) => {
    console.log('request:', JSON.stringify(event, undefined, 2));
    return {
        statusCode: 200,
        headers: { 'Content-Type': 'text/plain' },
        body: `Hello, CDK! You've hit ${event.path}\n`,
    };
};