我想从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 /
}
}下面的全部错误似乎表明,至少部分问题是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, {发布于 2021-03-11 18:45:47
当CDK依赖项的版本处于不同版本时,通常会发生此错误Argument of type 'SomeClass' is not assignable to parameter of type 'ISomeClass'。为了解决这个问题,我们需要将所有的依赖项都放到同一个版本中。
删除package-lock.json
node_modules文件夹
npm install。
发布于 2022-03-23 02:46:41
另一个解决方案是像这样导入包:
const lambda = require('@aws-cdk/aws-lambda');但是代码质量检查程序(例如ESLINT)可能不喜欢它。
https://stackoverflow.com/questions/66572044
复制相似问题