首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解决AWS错误:“‘函数’类型的参数不能分配给‘IFunction’类型的参数”

如何解决AWS错误:“‘函数’类型的参数不能分配给‘IFunction’类型的参数”
EN

Stack Overflow用户
提问于 2021-03-10 20:01:34
回答 2查看 8.9K关注 0票数 16

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

代码语言:javascript
复制
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 /
  }
}

下面的全部错误似乎表明,至少部分问题是apigateway包有它自己的包是不兼容的。

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

代码语言:javascript
复制
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, {
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-03-11 18:45:47

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

删除package-lock.json

  • Ensure

  • node_modules文件夹

  • 删除package.json中的所有依赖项在依赖项之前使用相同的version.

  • Remove胡萝卜^符号,例如从“@aws/aws”:"^1.90.0“到”@aws/aws“:"1.90.0”,以避免不同的次要版本获取installed.

  • npm install

票数 41
EN

Stack Overflow用户

发布于 2022-03-23 02:46:41

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

代码语言:javascript
复制
const lambda = require('@aws-cdk/aws-lambda');

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66572044

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档