gpt4 book ai didi

aws-lambda - 如何解决 AWS CDK 错误 "Argument of type ' 函数'不可分配给 'IFunction' 类型的参数“

转载 作者:行者123 更新时间:2023-12-04 00:51:06 27 4
gpt4 key购买 nike

我想从 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, {

最佳答案

此错误 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
  • 关于aws-lambda - 如何解决 AWS CDK 错误 "Argument of type ' 函数'不可分配给 'IFunction' 类型的参数“,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66572044/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com