如何解决 AWS CDK 错误“'Function' 类型的参数不可分配给'IFunction' 类型的参数”

And*_*ham 5 aws-lambda aws-cdk

我想从https://docs.aws.amazon.com/cdk/latest/guide/serverless_example.html获取以下示例代码工作,但我得到“类型'函数'的参数不可分配给类型的参数‘IFunction’”错误。

import * as cdk from '@aws-cdk/core';
import * as apigateway from '@aws-cdk/aws-apigateway';
import * as lambda from '@aws-cdk/aws-lambda';

export default class ApiGatewayFunctionStack extends cdk.Stack {
  
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    
    super(scope, id, props);

    const handler = new lambda.Function(this, 'WidgetHandler', {
      runtime: lambda.Runtime.NODEJS_10_X, // So we can use async in widget.js
      code: lambda.Code.fromAsset('resources'),
      handler: 'widgets.main',
    });

    const api = new apigateway.RestApi(this, 'widgets-api', {
      restApiName: 'Widget Service',
      description: 'This service serves widgets.',
    });

    const getWidgetsIntegration = new apigateway.LambdaIntegration(handler, {
      requestTemplates: { 'application/json': '{ "statusCode": "200" }' },
    });

    api.root.addMethod('GET', getWidgetsIntegration); // GET /
  }
}
Run Code Online (Sandbox Code Playgroud)

下面的完整错误似乎表明问题的至少一部分可能是 aws-apigateway 包有自己的不兼容的包。

我不知道如何解决这个问题,所以非常感谢任何帮助。

test-deploy/ApiGatewayFunctionStack.ts:49:68 - error TS2345: Argument of type 'Function' is not assignable to parameter of type 'IFunction'.
  Types of property 'role' are incompatible.
    Type 'import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-iam/lib/role").IRole | undefined' is not assignable to type 'import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-apigateway/node_modules/@aws-cdk/aws-iam/lib/role").IRole | undefined'.
      Type 'import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-iam/lib/role").IRole' is not assignable to type 'import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-apigateway/node_modules/@aws-cdk/aws-iam/lib/role").IRole'.
        Types of property 'grant' are incompatible.
          Type '(grantee: import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-iam/lib/principals").IPrincipal, ...actions: string[]) => import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-iam/lib/grant").Grant' is not assignable to type '(grantee: import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-apigateway/node_modules/@aws-cdk/aws-iam/lib/principals").IPrincipal, ...actions: string[]) => import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-apigateway/node_modules/@aws-cdk/aws-iam/lib...'.
            Types of parameters 'grantee' and 'grantee' are incompatible.
              Type 'import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-apigateway/node_modules/@aws-cdk/aws-iam/lib/principals").IPrincipal' is not assignable to type 'import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-iam/lib/principals").IPrincipal'.
                Types of property 'addToPolicy' are incompatible.
                  Type '(statement: import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-apigateway/node_modules/@aws-cdk/aws-iam/lib/policy-statement").PolicyStatement) => boolean' is not assignable to type '(statement: import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-iam/lib/policy-statement").PolicyStatement) => boolean'.
                    Types of parameters 'statement' and 'statement' are incompatible.
                      Type 'import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-iam/lib/policy-statement").PolicyStatement' is not assignable to type 'import("D:/Users/andyb/Documents/github/agb-aws-functions/node_modules/@aws-cdk/aws-apigateway/node_modules/@aws-cdk/aws-iam/lib/policy-statement").PolicyStatement'.
                        Types have separate declarations of a private property 'action'.

49     const getWidgetsIntegration = new apigateway.LambdaIntegration(handler, {
Run Code Online (Sandbox Code Playgroud)

Bal*_*ala 11

Argument of type 'SomeClass' is not assignable to parameter of type 'ISomeClass'当 CDK 依赖项的版本处于不同版本时,通常会发生此错误。为了解决这个问题,我们需要将所有依赖项都带到同一个版本。

  • 删除node_modules文件夹
  • 删除 package-lock.json
  • 确保 package.json 中的所有依赖项都使用相同的版本。
  • 在依赖项之前删除胡萝卜 ^ 符号,例如从 "@aws-cdk/aws-lambda": "^1.90.0" 到 "@aws-cdk/aws-lambda": "1.90.0" ,以避免获得不同的次要版本安装。
  • npm install

  • 你拯救了我的启动! (3认同)