Aws-cdk: TS - Argument of type 'this' is not assignable to parameter of type 'Construct'

Created on 10 Aug 2018  Â·  19Comments  Â·  Source: aws/aws-cdk

I have a created a simple Stack that creates a VPC with three simple subnets. though I receive the error Argument of type 'this' is not assignable to parameter of type 'Construct' . When I use any other resource like S3 or SQS I do not receive the error. As of now, it happens only with VpcNetwork. The typescript code is as follows

cdk version: 0.8.0 (build bb95676)
node version: 10.3.0

import * as cdk from '@aws-cdk/cdk'
import { VpcNetwork, SubnetType } from '@aws-cdk/aws-ec2'

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

        new VpcNetwork(this, 'MyVPC', {
            cidr: '10.0.0.0/21',
            subnetConfiguration: [
              {
                cidrMask: 24,
                name: 'Ingress',
                subnetType: SubnetType.Public,
                natGateway: true,
              },
              {
                cidrMask: 24,
                name: 'Application',
                subnetType: SubnetType.Private,
              },
              {
                cidrMask: 28,
                name: 'Database',
                subnetType: SubnetType.Isolated,
              }
            ],
        });
    }
}

const app = new cdk.App(process.argv);
new MyStack(app, 'MyStack');
process.stdout.write(app.run());

Most helpful comment

Hi @benswinburne, I believe this is caused by the fact that your package.json file has mixed versions of the CDK (@aws-cdk/cdk is at 0.20.0 and @aws-cdk/aws-* are at 0.21.0):

{
  "dependencies": {
    "@aws-cdk/aws-ec2": "^0.21.0",
    "@aws-cdk/aws-ecs": "^0.21.0",
    "@aws-cdk/cdk": "^0.20.0"
  }
}

Try to upgrade everything to 0.21.0 and let us know if the issue still persists.

All 19 comments

This doesn't repro in 0.8.1, do you mind upgrading to the latest version and trying again? Let us know if you still encounter issues.

P.S. the natGateway property is not needed anymore...

@eladb works after upgrading to 0.8.1 . Thanks

Great!

I'm experiencing this problem too using 0.20.0. I was following the example found here

https://awslabs.github.io/aws-cdk/examples.html#step-3-create-a-fargate-service

and received a similar error to the one described above

# ~/Sites/playground/cdk-example î‚  master npm run build

> [email protected] build /Users/benswinburne/Sites/playground/cdk-example
> tsc

lib/cdk-example-stack.ts:10:36 - error TS2345: Argument of type 'this' is not assignable to parameter of type 'Construct'.
  Property '_escapePathSeparator' is missing in type 'CdkExampleStack' but required in type 'Construct'.

10     const vpc = new ec2.VpcNetwork(this, 'MyVpc', {
                                      ~~~~

  node_modules/@aws-cdk/aws-ec2/node_modules/@aws-cdk/cdk/lib/core/construct.d.ts:205:13
    205     private _escapePathSeparator;
                    ~~~~~~~~~~~~~~~~~~~~
    '_escapePathSeparator' is declared here.

lib/cdk-example-stack.ts:14:37 - error TS2345: Argument of type 'this' is not assignable to parameter of type 'Construct'.

14     const cluster = new ecs.Cluster(this, 'MyCluster', {
                                       ~~~~

lib/cdk-example-stack.ts:19:40 - error TS2345: Argument of type 'this' is not assignable to parameter of type 'Construct'.

19     new ecs.LoadBalancedFargateService(this, 'MyFargateService', {
                                          ~~~~


Found 3 errors.

I've pushed the example to a repo exactly as I have it

npm -v
6.4.1
node -v
v8.12.0

https://github.com/benswinburne/cdk-example

Hi @benswinburne, I believe this is caused by the fact that your package.json file has mixed versions of the CDK (@aws-cdk/cdk is at 0.20.0 and @aws-cdk/aws-* are at 0.21.0):

{
  "dependencies": {
    "@aws-cdk/aws-ec2": "^0.21.0",
    "@aws-cdk/aws-ecs": "^0.21.0",
    "@aws-cdk/cdk": "^0.20.0"
  }
}

Try to upgrade everything to 0.21.0 and let us know if the issue still persists.

I'm getting this same error while working through the AWS CDK Workshop.
Argument of type 'this' is not assignable to parameter of type 'Construct'.
Type 'HitCounter' is not assignable to type 'Construct'.
Types of property 'node' are incompatible.
Type 'import("/Users/dev_user/code/aws/cdk/cdk-workshop/node_modules/@aws-cdk/core/lib/construct").ConstructNode' is not assignable to type
'import("/Users/dev_user/code/aws/cdk/cdk-workshop/node_modules/@aws-cdk/aws-dynamodb/node_modules/@aws-cdk/core/lib/construct").ConstructNode'.
Types have separate declarations of a private property 'host'.ts(2345)

Following is the package.json
{
"name": "cdk-workshop",
"version": "0.1.0",
"bin": {
"cdk-workshop": "bin/cdk-workshop.js"
},
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"cdk": "cdk"
},
"devDependencies": {
"@types/node": "8.10.45",
"typescript": "^3.3.3333",
"ts-node": "^8.1.0",
"aws-cdk": "^1.0.0"
},
"dependencies": {
"@aws-cdk/aws-apigateway": "^1.0.0",
"@aws-cdk/aws-dynamodb": "^1.1.0",
"@aws-cdk/aws-lambda": "^1.0.0",
"@aws-cdk/aws-sns": "^1.0.0",
"@aws-cdk/aws-sns-subscriptions": "^1.0.0",
"@aws-cdk/aws-sqs": "^1.0.0",
"@aws-cdk/core": "^1.0.0"
}
}

The type seems to be specified properly. This seems to be a bug.

I'm getting this same error while working through the AWS CDK Workshop.
Argument of type 'this' is not assignable to parameter of type 'Construct'.
Type 'HitCounter' is not assignable to type 'Construct'.
Types of property 'node' are incompatible.
Type 'import("/Users/dev_user/code/aws/cdk/cdk-workshop/node_modules/@aws-cdk/core/lib/construct").ConstructNode' is not assignable to type
'import("/Users/dev_user/code/aws/cdk/cdk-workshop/node_modules/@aws-cdk/aws-dynamodb/node_modules/@aws-cdk/core/lib/construct").ConstructNode'.
Types have separate declarations of a private property 'host'.ts(2345)

Following is the package.json
{
"name": "cdk-workshop",
"version": "0.1.0",
"bin": {
"cdk-workshop": "bin/cdk-workshop.js"
},
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"cdk": "cdk"
},
"devDependencies": {
"@types/node": "8.10.45",
"typescript": "^3.3.3333",
"ts-node": "^8.1.0",
"aws-cdk": "^1.0.0"
},
"dependencies": {
"@aws-cdk/aws-apigateway": "^1.0.0",
"@aws-cdk/aws-dynamodb": "^1.1.0",
"@aws-cdk/aws-lambda": "^1.0.0",
"@aws-cdk/aws-sns": "^1.0.0",
"@aws-cdk/aws-sns-subscriptions": "^1.0.0",
"@aws-cdk/aws-sqs": "^1.0.0",
"@aws-cdk/core": "^1.0.0"
}
}

The type seems to be specified properly. This seems to be a bug.

Same error here

Me too, the error comes up on a super minimal:

new Queue(this, 'foo')

Exact some error

Argument of type 'this' is not assignable to parameter of type 'Construct'.
...
Types have separate declarations of a private property 'host'

As mentioned above, this is usually caused by having more than a single version of the CDK in your node modules.

  1. Make sure your package.json lists the same version.
  2. Nuke node_modules
  3. Run npm install again

Yeah, this was the issue. I did the init yesterday, and installed packages today, so they were mismatched:

aws-cdk=1.6.0,@aws-cdk/core=1.5.0 and this was causing the issue.

It'd be really great since this seems like a common issue for people that we had a nicer error message here, perhaps the cdk command could verify that dependencies are in sync since they appear to have hard dependencies on being the exact same versions?

The error was not obvious, and this thread didn't really help solve it, since the packages being mismatched was never discussed here, just someone upgrading from one version to the next solved it. This issue was the one that made me double check and upgrade all versions: https://github.com/aws/aws-cdk/issues/3416

It's a good idea but might be a bit tricky for us to handle at the CLI level because it is language agnostic. We are actually changing the way peer dependencies are defined (see #3783) so I hope that this will improve the experience a little.

At any rate, you are right. This is an annoying friction we are aware of and would look into ways to improve this experience.

You should absolutely re-open this issue as this clearly has not been adequately solved. We've incurred a lot of cost trying to wrangle between version mismatches and I'm not even sure that fully diagnoses the issue of something as trivial as two separate versions of the Construct class.

This amounts to "configuration hell" and the responsible thing is to own that these issues exist or clearly state your assumptions in your documentation about what developers need to do to circumvent such issues in reliable ways. As of now, my conclusion is that building CDK applications in TypeScript is a waste of time (prefer JavaScript).

Yeah, this was the issue. I did the init yesterday, and installed packages today, so they were mismatched:

aws-cdk=1.6.0,@aws-cdk/core=1.5.0 and this was causing the issue.

It'd be really great since this seems like a common issue for people that we had a nicer error message here, perhaps the cdk command could verify that dependencies are in sync since they appear to have hard dependencies on being the exact same versions?

The error was not obvious, and this thread didn't really help solve it, since the packages being mismatched was never discussed here, just someone upgrading from one version to the next solved it. This issue was the one that made me double check and upgrade all versions: #3416

I have similar issue. I did cdk init then after few hours did npm install @aws-cdk/aws-ec2.
I realized that the problem is caused by different versions:

    "@aws-cdk/aws-ec2": "^1.32.2",
    "@aws-cdk/core": "1.32.1",

Just after few hours there is a minor update on the module. Problem solved by changing aws-cdk/core version to 1.32.2

Since I stumpled over this one, even in latest cdk:

  1. How to debug:

In working state, get the output of code that is a result of synth

CDKMetadata:
    Type: AWS::CDK::Metadata
    Properties:
      Modules: aws-cdk=1.32.0,@aws-cdk/aws-cloudwatch=1.32.2,@aws-cdk/aws-ec2=1.32.2,@aws-cdk/aws-events=1.32.2,@aws-cdk/aws-iam=1.32.2,@aws-cdk/aws-kms=1.32.2,@aws-cdk/aws-logs=1.32.2,@aws-cdk/aws-s3=1.32.2,@aws-cdk/aws-ssm=1.32.2,@aws-cdk/core=1.32.2,@aws-cdk/cx-api=1.32.2,@aws-cdk/region-info=1.32.2,jsii-runtime=node.js/v10.16.3

Aws CDK has a mismatch (1.32.0) to the other modules (1.32.2).

  1. You might want to fix this, by not using the circumflex '^' in your package.json.

you might not only want to nuke your node_modules folder, but as well the package-lock.json

This is a little side effect of the way typings work and how they evolve. An argument could be made, that a minor update is not the right thing as soon as a typing changed.

I hope this helps if you stumble over this problem.

@sebsonjura thanks for the fix. AWS documentation perhaps needs to mention this explicitly as it is not in the typescript workshop. It would be useful. Thanks again!

Just to add, for anyone still struggling even after the helpful comments above:

After you nuke node_modules and package-lock.json, you then have to change all the versions for cdk things in package.json UP TO THE NEW VERSION, not down to old one. I tried changing it down (since I'd prefer not to be forced to use the latest minor release within minutes of it being released) and that didn't work. Upwards and onwards.

Maybe a version mismatch check could be added to cdk doctor?

I updated all my dependencies to 1.55 and it worked like a charm!

Please try to use 1.56.0 without the ^ whenever using CDK.

Also your devDependency links to 1.45.0

Was this page helpful?
0 / 5 - 0 ratings