【问题标题】:AWS cdk typescript shows error construct scopeAWS cdk typescript 显示错误构造范围
【发布时间】:2020-07-26 11:02:10
【问题描述】:

我正在使用 AWS CDK TypeScript。我正在尝试在 cdk 中创建一个认知用户池。但它在“this”处显示以下警告,

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

我的代码如下,

import * as cdk from '@aws-cdk/core';
import { UserPool } from '@aws-cdk/aws-cognito'

export class CognitoCreation extends cdk.Construct {
  constructor(scope: cdk.Construct, id: string) {
    super(scope, id);
    new UserPool(this, 'myuserpool', {
        userPoolName: 'my-userpool',
    });    
  }
}

请查看附件。

我的 package.jso 如下所示,

{
  "name": "******",
  "version": "0.1.0",
  "bin": {
    "regional-infrastructure": "bin/*****.js"
  },
  "scripts": {
    "build": "tsc",
    "watch": "tsc -w",
    "test": "jest",
    "cdk": "cdk"
  },
  "devDependencies": {
    "@aws-cdk/assert": "1.53.0",
    "@types/jest": "^25.2.1",
    "@types/node": "10.17.5",
    "jest": "^25.5.0",
    "ts-jest": "^25.3.1",
    "aws-cdk": "1.53.0",
    "ts-node": "^8.1.0",
    "typescript": "~3.7.2"
  },
  "dependencies": {
    "@aws-cdk/aws-cloudformation": "^1.53.0",
    "@aws-cdk/aws-cognito": "^1.54.0",
    "@aws-cdk/aws-dynamodb": "^1.53.0",
    "@aws-cdk/aws-elasticsearch": "^1.53.0",
    "@aws-cdk/aws-iam": "^1.53.0",
    "@aws-cdk/aws-lambda": "^1.53.0",
    "@aws-cdk/aws-lambda-event-sources": "^1.53.0",
    "@aws-cdk/aws-sns": "^1.53.0",
    "@aws-cdk/aws-sns-subscriptions": "^1.53.0",
    "@aws-cdk/core": "1.53.0",
    "@aws-cdk/custom-resources": "^1.53.0",
    "aws-sdk": "^2.716.0",
    "source-map-support": "^0.5.16"
  }
}

我已经运行了“npm install”,并且在节点模块中添加了 cognito 和其他模块。我尝试用“范围”改变“这个”,但这对我不起作用。我的cdk版本是1.46.0 (build 63860b2)

【问题讨论】:

  • 您能否添加更多信息:使用的 CDK 版本,使用的依赖项列表(例如粘贴 package.json),您是否运行了“npm install”?如上所述尝试用户池时我没有遇到任何问题。
  • 请在上述问题中找到 package.json 和 cdk 版本。我确实运行了“npm install”。
  • 你的课程应该扩展cdk.Stack而不是cdk.Construct吗?
  • 但是我想将此资源创建为与其他资源相同的堆栈的一部分...我仍然尝试将 cdk.Construct 更改为 cdk.Stack 但没有进行任何更改。

标签: typescript amazon-web-services aws-cdk


【解决方案1】:

您已经找到了解决方案,所以我只是将其发布以供后代使用。这个问题似乎出现了很多;我已经处理了很多次,这可能会非常令人沮丧。一般建议是删除 node_modules,然后重新安装,确保所有内容都有匹配的版本。如果使用 CDK 的全局安装然后在 package.json 中调用它,也要小心。在这种情况下,您需要删除本地安装或在cdk 命令之前运行npx

https://github.com/aws/aws-cdk/issues/7280

【讨论】:

    【解决方案2】:

    我的版本存在一些问题。 使所有版本的aws-cdkaws-* 相同解决了这个问题。 我确实按照以下步骤操作:

    • cd 到你的项目路径
    • npx npm-check-updates -u
    • npm 安装
    • 重启你的IDE

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-10
      • 2018-12-14
      • 2017-01-16
      • 2023-01-02
      • 2021-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多