简体   繁体   中英

Extend ApiGateProxyEvent type of aws-lambda

I want to extend the ApiGateProxyEvent of aws-lambda in d.ts.

I tried something like this but it replaced the whole APIGatewayProxyEvent instead of extending them.

declare namespace AWSLambda {
  interface APIGatewayProxyEvent {
    body: any;
    user: any;
  }
}

Next I tried this:

import 'aws-lambda';

declare module 'aws-lambda' {
  interface APIGatewayProxyEvent {
    body: any;
    user: any;
  }
}

but I got this error:

Duplicate identifier 'APIGatewayProxyEvent'.ts(2300)
api-gateway-proxy.d.ts(60, 13): 'APIGatewayProxyEvent' was also declared here.

My typescript version is 4.7.4 and here is my tsconfig.json

{
  "compilerOptions": {
    "target": "ES2022",
    "esModuleInterop": true,
    "moduleResolution": "node",
    "forceConsistentCasingInFileNames": true,
    "isolatedModules": true,
    "baseUrl": "."
  }
}

According to the source of aws-lambda , the namespace will be AWSLambda .

Then what you need to do in .d.ts file will look like this:

custom.d.ts

declare namespace AWSLambda {
  export interface APIGatewayProxyEvent {
    // body: any; // body should be a string, maybe you need another property name
    user: any;
  }
}

I was using following notation until version 8.10.45 (maybe few patches more) of @types/aws-lambda :

// without import 'aws-lambda'

declare module 'aws-lambda' {
  export interface APIGatewayProxyEvent {
    user: any;
  }
}

Since version 8.10.61 they changed APIGatewayProxyEvent from interface to type, so I needed to change it to:

// without import 'aws-lambda'

declare module 'aws-lambda' {
  export interface APIGatewayProxyEventBase<TAuthorizerContext> {
    user: any;
  }
}

Works with latest version too (@types/aws-lambda@8.10.102)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM