AWS cdk 打字稿显示错误构造范围

AWS cdk typescript shows error construct scope

我正在使用 AWS CDK TypeScript。我正在尝试在 cdk 中创建一个 cognito 用户池。但它在“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)

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

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

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

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