node.js - Amazon CDK : Incorrect Construct Type even though it is extended from cdk. 构造?

标签 node.js typescript aws-cloudformation aws-cdk

我正在尝试使用自定义域部署 API 网关。 (没有函数,只是其他堆栈可以导入和使用的通用 API 网关)

bin/api-app.ts:

import { APIAppStack } from '../lib/api-app-stack';

export class MyAPIAppStack extends cdk.Stack {
    constructor(parent: cdk.App, name: string, props: cdk.StackProps) {
        super(parent, name, props);

        new APIAppStack(this, 'APIApp', {
            domainName: this.node.tryGetContext('domain'),
            siteSubDomain: this.node.tryGetContext('subdomain'),
        });
   }
}

const app = new cdk.App();

new MyAPIAppStack(app, 'APIAppStack'+(((process.env.subdomain || "").length > 0)?"-"+process.env.subdomain:""), { env: {
    region: process.env.AWS_DEFAULT_REGION,
    account: process.env.CDK_DEFAULT_ACCOUNT
}});

app.synth();

这是我的构造: lib/api-app-stack:

export interface APIAppProps {
    domainName: string;
    siteSubDomain: string;
}

export class APIAppStack extends Construct {
    constructor(parent: Construct, name: string, props: APIAppProps) {
        super(parent, name);
        const zone = route53.HostedZone.fromLookup(this, 'Zone', { domainName: props.domainName });
        const siteDomain = "api." + props.domainName;
        new cdk.CfnOutput(this, 'API', { value: 'https://' + siteDomain });

        // ... removed code for brevity. All works fine.
        // My error is below: (I am not able to create the ApiGateway itself)
        const api = new apigw.RestApi(this, 'APIApp', {
            restApiName: 'API App',
            description: "The API App exposes a common endpoint for the Apps to consume.",
            deployOptions: {
                accessLogDestination: accessLogDestination,
                accessLogFormat: apigw.AccessLogFormat.jsonWithStandardFields(),
                loggingLevel: apigw.MethodLoggingLevel.INFO,
                dataTraceEnabled: true,
                tracingEnabled: true,
                metricsEnabled: true,
                description: "Prod Stage"
            }
        });

错误位于 apigw.RestApi(this:

Argument of type 'this' is not assignable to parameter of type 'Construct'.
  Type 'APIAppStack' is not assignable to type 'Construct'.
    Property 'onValidate' is protected but type 'Construct' is not a class derived from 'Construct'.ts(2345)

我是 TypeScript 新手,我知道可能存在属性不匹配的情况,但我从 GitHub 上的 StaticSite 示例代码派生了此代码。 。我什至看到 ApiAppStack 派生自 Construct。我错过了什么吗?

编辑1: 这是我的package.json:

{
  "name": "api-app",
  "version": "0.1.0",
  "bin": {
    "frontend-app": "bin/api-app.js"
  },
  "scripts": {
    "build": "tsc",
    "watch": "tsc -w",
    "cdk": "cdk"
  },
  "devDependencies": {
    "aws-cdk": "1.38.0",
    "@aws-cdk/assert": "1.38.0",
    "@types/node": "10.17.5",
    "jest": "^25.5.0",
    "ts-node": "^8.1.0",
    "typescript": "~3.7.2",
    "@aws-cdk/aws-certificatemanager": "*",
    "@aws-cdk/aws-cloudfront": "*",
    "@aws-cdk/aws-iam": "*",
    "@aws-cdk/aws-route53": "*",
    "@aws-cdk/aws-route53-targets": "*",
    "@aws-cdk/aws-s3": "*",
    "@aws-cdk/aws-s3-deployment": "*",
    "@aws-cdk/core": "*"
  },
  "dependencies": {
    "@aws-cdk/aws-apigateway": "^1.47.1",
    "@aws-cdk/aws-sns": "1.38.0",
    "@aws-cdk/aws-sns-subscriptions": "1.38.0",
    "@aws-cdk/aws-sqs": "1.38.0",
    "@aws-cdk/core": "1.38.0"
  }
}

最佳答案

CDK 在小版本升级中不具有向后兼容性。

这意味着您的包中的cdk.Construct

"@aws-cdk/core": "1.38.0"

apigw.RestApi 不兼容,后者使用的版本至少为 1.47:

"@aws-cdk/aws-apigateway": "^1.47.1"

要解决此问题,请将 package.json 中的所有 @aws-cdk 包更改为相同版本,并且不要忘记删除 ^或者至少替换为 ~

然后只需重新安装依赖项:npm install

关于node.js - Amazon CDK : Incorrect Construct Type even though it is extended from cdk. 构造?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62672588/

相关文章:

javascript - 在 Typescript 中导入 Rx.Disposable

linux - 尝试为 Cloudformation 创建/更新 bash 脚本时出现 AlreadyExistsException 错误

javascript - NodeJS 将图像流转换为 base64 给出无效数据

javascript - Botmaker 解决 Qnamaker 后续问题

typescript - 订阅推送通知服务后添加管道

amazon-ec2 - 使用本地环境替换cloudformation模板中的变量

aws-cloudformation - 如何在 aws cloudformation 中共享两个参数之间允许的值

javascript - 通过 express 从 mongo 获取数据,构建对象,并发送给 React

html - 在 React 中覆盖 RSS 提要样式

typescript - 整数(文字)联合类型,以 NaN 作为有效返回值