“this”类型的参数不可分配给“Construct”类型的参数

时间:2021-06-01 12:43:42

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

即使我有相同版本的 cdk 依赖项,我也很难弄清楚这里的问题是什么。 const identityPool = new cognito.CfnIdentityPool(this, "IdentityPool", {

Argument of type 'this' is not assignable to parameter of type 'Construct'.
  Type 'MyStack' is not assignable to type 'Construct'.
    Types of property 'node' are incompatible.
      Type 'import("(**/node_modules/@aws-cdk/core/lib/construct-compat").ConstructNode' is not assignable to type 'import("**/node_modules/@aws-cdk/aws-appsync/node_modules/@aws-cdk/core/lib/construct-compat").ConstructNode'.
        Types have separate declarations of a private property 'host'.ts(2345)
        
      
"devDependencies": {
      "@aws-cdk/assert": "1.106.0",
      "@types/aws-lambda": "^8.10.76",
      "@types/aws-sdk": "^2.7.0",
      "@types/jest": "^26.0.10",
      "@types/node": "^15.6.1",
      "aws-cdk": "1.106.0",
      "jest": "^26.4.2",
      "ts-jest": "^26.2.0",
      "ts-node": "^9.0.0",
      "typescript": "~3.9.7"
  },
  "dependencies": {
      "@aws-cdk/aws-appsync": "^1.106.0",
      "@aws-cdk/aws-cognito": "^1.106.0",
      "@aws-cdk/aws-dynamodb": "^1.106.0",
      "@aws-cdk/aws-lambda": "^1.106.0",
      "@aws-cdk/aws-s3": "^1.106.0",
      "@aws-cdk/core": "1.106.0",
      "graphql-scalars": "^1.9.3",
      "source-map-support": "^0.5.16",
      "yaml": "^1.10.2"
  }
}

复制步骤

import * as appsync from "@aws-cdk/aws-appsync";
import * as cognito from "@aws-cdk/aws-cognito";
import * as ddb from '@aws-cdk/aws-dynamodb';
import * as iam from "@aws-cdk/aws-iam";
import * as lambda from '@aws-cdk/aws-lambda';
import * as s3 from "@aws-cdk/aws-s3";
import * as cdk from "@aws-cdk/core";


export class MyStack extends cdk.Stack {
   private readonly stage: Stage;
   private readonly context: string;

  private namePrefix() {
    return `${this.stage}-${this.context}`;
  }

  constructor(scope: cdk.Construct, id: string, props: MyStackProps) {
    super(scope, id, props);
    this.stage = props.stage;
    this.context = props.context;
    
     const userPool = new cognito.UserPool(
      this,
      this.namePrefix() + "-user-pool",
      {
        userPoolName: this.namePrefix() + "-user-pool",
        selfSignUpEnabled: true,
        removalPolicy: cdk.RemovalPolicy.DESTROY,
        accountRecovery: cognito.AccountRecovery.PHONE_AND_EMAIL,
        userVerification: {
          emailStyle: cognito.VerificationEmailStyle.CODE,
        },
        autoVerify: {
          email: true,
        },
        standardAttributes: {
          email: {
            required: true,
            mutable: true,
          },
        },
      }
    );
       const userPoolClient = userPool.addClient(
      this.namePrefix() + "-user-pool-client",
      {
        userPoolClientName: this.namePrefix() + "-user-pool-client",
        oAuth: {
          flows: { authorizationCodeGrant: true, implicitCodeGrant : true },
          scopes: [cognito.OAuthScope.PROFILE],
          callbackUrls: ["http://localhost:4200/callback"],
        },
        authFlows : {
          userPassword : true,
          userSrp : true
        },
        supportedIdentityProviders: [
          cognito.UserPoolClientIdentityProvider.COGNITO,
        ],
      }
    );

   


    const identityPool = new cognito.CfnIdentityPool(this, "IdentityPool", {
      allowUnauthenticatedIdentities: false, // Don't allow unathenticated users
      cognitoIdentityProviders: [
        {
          clientId: userPoolClient.userPoolClientId,
          providerName: userPool.userPoolProviderName,
        },
      ],
    });
    
  }
 }

环境

  • **CDK CLI 版本:1.106.1
  • Node.js 版本: v16.2.0
  • **操作系统:Ubuntu
  • 语言(版本): Typescript(4.3.2)

1 个答案:

答案 0 :(得分:0)

您似乎有两个不同版本的 @aws-cdk/core 库。首先确保是问题所在:

yarn why @aws-cdk/core
# or
npm why @aws-cdk/core

将显示您项目中安装的所有 @aws-cdk/core 版本以及使用它们的包。如果存在多个版本,请使用通用拼写:

rm -rf node_modules

rm yarn.lock
yarn install
# or
rm package-lock.json
npm install

如果这仍然没有帮助,并且该软件包仍有两个版本,请使用 yarn resolutions 强制单一版本解析。