'this' is not assignable to parameter of type 'Construct' in CDK

29 July 2020 — Written by Edwin
#CDK#AWS#Typescript

I was working on one of my AWS-CDK apps in Typescript when I got squiggly lines under every 'this' in my code.

The issue turned out was that I installed/updated a CDK package and now had different versions of my CDK packages installed.

Take a look into package.json in your project folder:

  "dependencies": { 
    "@aws-cdk/aws-dynamodb": "^1.54.0",
    "@aws-cdk/aws-iam": "^1.54.0",
    "@aws-cdk/aws-lambda": "^1.55.0",    "@aws-cdk/core": "^1.54.0",
    "source-map-support": "^0.5.16"
  }

"@aws-cdk/aws-lambda" is installed in version "1.55.0" while the other packages are installed at version "1.54.0".

This happens when the CDK is updated and you probably installed the latest version with:

$ npm install @aws-cdk/aws-lambda

This command will always install the latest version of the npm package.

Solution

You need to install all CDK packages in the same version to work.

Open your package.json and under "dependencies" set all @aws-cdk packages to the same version.

  "dependencies": { 
    "@aws-cdk/aws-dynamodb": "^1.54.0",
    "@aws-cdk/aws-iam": "^1.54.0",
    "@aws-cdk/aws-lambda": "^1.54.0",    "@aws-cdk/core": "^1.54.0",
    "source-map-support": "^0.5.16"
  }

Next run

$ npm install

This will install all your packages in package.json in the version that you selected.

Automate

If you like to do this step without changing the versions you can use RocketCDK.

Run this command to update all your CDK packages to the latest version:

$ npx rocketcdk up

or specify a version to install a definite version:

$ npx rocketcdk up -v 1.54.0

And squiggly lines gone! 🚀

© 2020 Built with ❤️