【发布时间】: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