Borislav Hadzhiev
Last updated: Apr 14, 2022
Check out my new book
The reason we get the "Argument of type is not assignable to parameter of type Construct" error in CDK is because we have different versions of CDK packages in the same CDK project.
Different versions of CDK packages are often incompatible with each other, for example:
{ "dependencies": { "@aws-cdk/aws-dynamodb": "^1.100.0", "@aws-cdk/aws-iam": "^1.101.0", } }
In the code snippet, the versions of the packages differ and this causes the error.
In order to solve the "Argument of type is not assignable to parameter of type Construct" error in CDK, we have to:
Update our CDK packages in the dependencies
section in package.json
to
the same version.
^
(caret) symbol prefix from the versions.Npm adds the ^
symbol by default. The ^
symbol tells npm to install the
version we have specified or install the latest minor or patch version.
When using CDK packages, we want to lock the version down, therefore we
remove the ^
prefix.
node_modules
folder and the package-lock.json
file:rm -rf node_modules rm package-lock.json
npm install
The next time you install a CDK package, use the --save-exact
flag, so that
you lock down the version of the package and tell npm
to omit the ^
symbol
prefix.
To install a specific version of a CDK package and lock it:
npm install --save-exact @aws-cdk/aws-s3@1.101.0
To install the latest version of a CDK package and lock it:
npm install --save-exact @aws-cdk/aws-s3@latest
If you look at the dependencies
section in your package.json
file, after
installing a module with the --save-exact
flag, you will see that the ^
(caret) symbol is not included and the version is locked down.
{ "dependencies": { "@aws-cdk/aws-s3": "1.101.0" } }
package.json
file, before installing new CDK packages. The CDK library gets updated almost daily, so unfortunately it's not as simple as installing the latest version, when adding new dependencies.