title: AWS Lambda with typescript getting Cannot read property of undefined inside async handler
body: <p>Typescript newbie here. I am working on an AWS Lambda function by using typescript with classes. I am exporting an <code>async</code> handler at the end. When I invoke my function from AWS SAM CLI then I am getting error of;</p>
  <blockquote>
  <p>{&quot;errorType&quot;:&quot;TypeError&quot;,&quot;errorMessage&quot;:&quot;Cannot read property 'test' of undefined&quot;,&quot;stack&quot;:[&quot;TypeError: Cannot read property 'test' of undefined&quot;,&quot;    at Runtime.handler (/var/task/src/lambda/create-cost-lambda.js:12:56)&quot;,&quot;    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)&quot;]}</p>
  </blockquote>
  <p><strong>create-cost-lambda.ts</strong></p>
  <pre><code>class CreateCostLambda {
      private readonly foobarRepository: FoobarRepository;
  
      constructor() {
          this.foobarRepository = new FoobarRepository();
      }
  
      async handler(event: APIGatewayProxyEventV2) : Promise&lt;APIGatewayProxyResultV2&gt; {
          const result = await this.foobarRepository.test();
          console.log(result);
  
          return {
              body: JSON.stringify(result),
              statusCode: 200,
          };
      }
  }
  
  export const { handler } = new CreateCostLambda();
  </code></pre>
  <p>Here is a very basic class represents a repository.</p>
  <p><strong>foobar-repository.ts</strong></p>
  <pre><code>export class FoobarRepository {
      private readonly awesomeValue: string;
  
      constructor() {
          this.awesomeValue = 'John Doe';
      }
  
      async test(): Promise&lt;string&gt; {
          return this.awesomeValue;
      }
  }
  </code></pre>
  <p>I am almost sure it is because of the way I am exporting the handler and how <code>aws-sam</code> internally runs the handler. But I might be wrong and it can be typescript thing that I am missing. Please let me know if you need more information and thanks a lot for the help!</p>