簡體   English   中英

為什么 Typescript 顯示已棄用 Lambda 處理程序的事件?

[英]Why does Typescript shows event deprecated for Lambda Handler?

我正在嘗試在 typescript 環境中定義 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.

但是,對於相同的代碼,當我沒有單獨定義 function 時,它可以工作。

    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`,
  };
};

我認為錯誤不在於 function 定義本身,而在於你如何導出它。

您必須導出function而不是調用它:

錯誤的:

exports.handler = sampleFunc(event);

正確的:

exports.handler = sampleFunc;

您也可以直接導出 function:

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`,
    };
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM