gpt4 book ai didi

typescript - AWS CDK - 构造函数参数不匹配

转载 作者:行者123 更新时间:2023-12-03 07:33:16 25 4
gpt4 key购买 nike

在浏览 AWS CDK 教程时遇到错误。构造函数 S3.Bucket 需要一个构造函数,但扩展 cdk.Stack 的类似乎没有实现 Construct。它确实扩展了 CoreConstruct。不确定 Construct 和 CoreConstruct 是如何关联的。下面是源代码和 const bucket = new s3.Bucket(**this**, "SampleBucket", { 行中的“this”抛出错误。

import * as cdk from "@aws-cdk/core";
import * as s3 from "aws-cdk-lib/aws-s3";

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

const bucket = new s3.Bucket(this, "SampleBucket", {
encryption: s3.BucketEncryption.S3_MANAGED,
});

const output = new cdk.CfnOutput(this, "SampleBucketNameExport", {
value: bucket.bucketName,
exportName: "SampleBucketName",
});
console.log(output);
}
}

错误是:

Argument of type 'this' is not assignable to parameter of type 'Construct'.
Type 'CdkSampleStack' is not assignable to type 'Construct'.
Types of property 'node' are incompatible.
Type 'ConstructNode' is missing the following properties from type 'Node': _locked, _children, _context, _metadata, and 6 more.ts(2345)

知道出了什么问题吗?

预先感谢您的帮助。

最佳答案

所以你的问题是你在这里混合版本。 aws-cdk-lib 是 AWS CDK 的 v2。 @aws-cdk/* 库(例如 @aws-cdk/core)是该库的 v1 版本。因此,在您的示例中,您应该删除对 @aws-cdk/core 的依赖关系,而可以从 aws-cdk-lib、IE

导入所有内容
// The top level namespace includes all stuff that used to be in `@aws-cdk/core`, see the docs here https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib-readme.html
import * as cdk from "aws-cdk-lib";
import * as s3 from "aws-cdk-lib/aws-s3";
// v2 no longer wraps the interface with it's own thing so just use `constructs.Construct` directly
import { Constructs } from 'constructs';

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

const bucket = new s3.Bucket(this, "SampleBucket", {
encryption: s3.BucketEncryption.S3_MANAGED,
});

const output = new cdk.CfnOutput(this, "SampleBucketNameExport", {
value: bucket.bucketName,
exportName: "SampleBucketName",
});
console.log(output);
}
}

关于typescript - AWS CDK - 构造函数参数不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74603804/

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