'this' 类型的参数不可分配给参数 'Construct'

Argument of type 'this' not assignable to parameter 'Construct'

我正在尝试将 lambda 函数调用到 'sample app' 堆栈中,但它给了我一个错误,因为我正在尝试向它传递 'this'.

的参数

这是我的 lambda 函数

export async function handler(event) {
    console.log("request:", JSON.stringify(event, undefined, 2));
    return {
        statusCode: 200,
        headers: { "Content-Type": "text/plain" },
        body: `Hello, CDK! You've hit ${event.path}\n`
    };
};

这里是 'app' 调用那个函数

//import sns = require('@aws-cdk/aws-sns');
//import subs = require('@aws-cdk/aws-sns-subscriptions');
//import sqs = require('@aws-cdk/aws-sqs');
import cdk = require('@aws-cdk/core');
import lambda = require('@aws-cdk/aws-lambda');

//Exports class from other file much like a function

export class CdkWorkshopStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) { 
    super(scope, id, props);

    // Describes an AWSLambda Resource
    const hello = new lambda.Function (this, 'HelloHandler', {
      runtime: lambda.Runtime.NODEJS_8_10,    //execution environment
      code: lambda.Code.asset('lambda'),   // code loaded from the "lambda" directory
      handler: 'hello.handler'                // file is "hello", function is "handler"
    });
  }
}

我得到的错误是:


lib/cdk-workshop-stack.ts:31:39 - error TS2345: Argument of type 'this' is not assignable to parameter of type 'Construct'.
  Type 'CdkWorkshopStack' is not assignable to type 'Construct'.
    Types of property 'node' are incompatible.
      Type 'import("/Users/aroe/cdk-workshop/node_modules/@aws-cdk/core/lib/construct").ConstructNode' is not assignable to type 'import("/Users/aroe/cdk-workshop/node_modules/@aws-cdk/aws-lambda/node_modules/@aws-cdk/core/lib/construct").ConstructNode'.
        Types have separate declarations of a private property 'host'.

31     const hello = new lambda.Function(this, 'HelloHandler', {
                                         ~~~~

[1:24:08 PM] Found 1 error. Watching for file changes.

最后我使用的是 Node 版本 v13.3.0

构造定义对我来说很合适。如果各种 cdk 模块的版本不同,就会发生该错误;见 Argument of type 'this' is not assignable to parameter of type 'Construct' 的一个例子。尝试 运行 npm update;看看是否能解决问题。

通过简单地忽略 'this' 错误和 运行 lambda 即可解决此问题。如果它是一个实际的 node/JS 程序,我认为它不会起作用。然而,当使用 CDK 原生 TypeScript 时,它会让你感到很舒服。

我遇到了同样的错误,因为 @aws-cdk/aws-lambda@aws-cdk/aws-sns-subscriptions 的安装版本不同,

aws-sns-subscriptions依赖于相同版本的aws-lambda

当我降级 @aws-cdk/aws-lambda 版本时,错误消失了!

您还可以将所有 aws-cdk 个包更新为相同版本。

就我而言,尽管我使用了 cdk init app --language=typescript 项目,但该项目使用旧的遗留 aws-cdk 库进行引导。

因此,当我开始使用 @aws-cdk 命名空间包中的 Typescript 库时,我收到了这个错误。

所以我不得不替换 @aws-cdk/core:

中对 StackConstruct 等的所有引用
import * as cdk from '@aws-cdk/core';
import * as sqs from '@aws-cdk/aws-sqs';
import * as lambda from '@aws-cdk/aws-lambda'
import { SqsEventSource } from '@aws-cdk/aws-lambda-event-sources';

export class WorkerCdkStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // example resource
    const queue = new sqs.Queue(this, 'WorkerQueue', {
      queueName: 'WorkerQueue',
    });

    // processor function
    const processor = new lambda.Function(this, 'QueueProcessor', {
      code: lambda.Code.fromAsset('lambda'),
      handler: 'processor.handler',
      functionName: 'QueueProcessor',
      runtime: lambda.Runtime.NODEJS_12_X,
    });

    // map processor to queue
    processor.addEventSource(new SqsEventSource(queue, {
      batchSize: 10, // default
      reportBatchItemFailures: true, // default to false
    }));
  }
}