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

时间:2020-07-26 11:02:10

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

我正在使用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',
    });    
  }
}

请检查enter image description here附件。

我的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和其他模块。我尝试用“ scope”更改“ this”,但这对我没有用。我的cdk版本是1.46.0 (build 63860b2)

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

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

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