Home > Software design >  Why does Typescript shows event deprecated for Lambda Handler?
Why does Typescript shows event deprecated for Lambda Handler?

Time:03-27

I am trying to define lambda handler in a typescript environment.

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);

For the event it is strikethrough ( couldn't format in questions) and the compiler say it is deprecated.

deprecation message :

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

However for the same code when I am not defining function separately it works.

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

CodePudding user response:

I think the error is not with the function definition itself but how you export it.

You have to export the function not call it:

Wrong:

exports.handler = sampleFunc(event);

Right:

exports.handler = sampleFunc;

You could also export the function directly:

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