【问题标题】:How to resolve AWS CDK error "Argument of type 'Function' is not assignable to parameter of type 'IFunction'"如何解决 AWS CDK 错误“'Function' 类型的参数不可分配给 'IFunction' 类型的参数”
【发布时间】:2021-03-10 20:01:34
【问题描述】:

我想从https://docs.aws.amazon.com/cdk/latest/guide/serverless_example.html 获取以下示例代码,但我收到“'Function' 类型的参数不可分配给'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 /
  }
}

下面的完整错误似乎表明至少部分问题可能是 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, {

【问题讨论】:

  • 如果我删除 /agb-aws-functions/node_modules/@aws-cdk/aws-apigateway/node_modules 文件夹,问题就会消失。但是,npm i 会导致此文件夹重新出现并再次导致问题。
  • 问题似乎是安装 @aws-cdk/aws-apigateway 包会创建一个 node_modules 子文件夹,其中包含不兼容的接口版本。在 GitHub 上构建时,发生了同样的错误,我不知道为什么要创建这个 node_modules 子文件夹以及如何抑制它。
  • 你能添加你的 package.json 吗?通常发生在 cdk 依赖项处于不同版本时。
  • 去掉 package.json 中的 ^ 符号,用于依赖 "@aws-cdk/aws-lambda": "^1.90.0""@aws-cdk/aws-lambda": "1.90.0",删除 package-lock.json,删除 node_modules 和 npm install 再试一次。
  • 感谢您的建议。我的 package.json 可以在这里找到:github.com/andybalham/agb-aws-functions/blob/main/package.json。稍后我会尝试您的建议。

标签: aws-lambda aws-cdk


【解决方案1】:

此错误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

【讨论】:

  • 非常感谢,伙计,你救了我几天的调试,我正在删除模块,忘记删除 package-lock.json
  • 你拯救了我的创业!
  • 我尝试了同样的获取:> npm install npm ERR!代码 ERESOLVE npm 错误! ERESOLVE 无法解析依赖树 npm ERR! npm 错误!解决时:cdk@0.1.0 npm ERR!发现:typescript@3.7.7 npm ERR! node_modules/typescript npm 错误! dev typescript@"~3.7.2" 来自根项目 npm ERR! npm 错误!无法解决依赖关系:npm ERR! peer typescript@">=3.8
  • npm 错误!修复上游依赖冲突,或者重试npm ERR!此命令带有 --force 或 --legacy-peer-deps npm ERR!接受不正确的(并且可能被破坏的)依赖解决方案。 npm 错误! npm 错误!有关完整报告,请参阅 /Users/ashishkarpe/.npm/eresolve-report.txt。 npm 错误!可以在以下位置找到此运行的完整日志:npm ERR! /Users/ashishkarpe/.npm/_logs/2021-10-29T06_36_09_516Z-debug.log
  • 我的详细问题在这里stackoverflow.com/questions/69732793/…
【解决方案2】:

另一种解决方案是像这样导入包:

const lambda = require('@aws-cdk/aws-lambda');

但代码质量检查器(例如 ESLINT)可能不喜欢它。

【讨论】:

    猜你喜欢
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2020-01-05
    • 2022-06-24
    • 2021-03-01
    • 2021-08-20
    • 2021-09-09
    • 2022-01-17
    相关资源
    最近更新 更多