Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TS2345: type 'Construct' is not a class derived from 'Construct'

Tags:

typescript

Following the tutorial on this page: https://cdkworkshop.com/20-typescript/40-hit-counter/300-resources.html

I believe this, passed to the Table constructor, should be HitCounter, a sub-constructor of cdk.Construct

It returns this lint:

this: this
Argument of type 'this' is not assignable to parameter of type 'Construct'.
  Type 'HitCounter' is not assignable to type 'Construct'.
    Property 'onValidate' is protected but type 'Construct' is not a class derived from 'Construct'.ts(2345)

I also get an internal server error on this step. Anyone know what the issue is?

import * as cdk from "@aws-cdk/core";
import * as lambda from "@aws-cdk/aws-lambda";
import * as dynamodb from "@aws-cdk/aws-dynamodb";

export interface HitCounterProps { downstream : lambda.IFunction; }

export class HitCounter extends cdk.Construct {
   public readonly handler: lambda.Function;

   constructor(scope: cdk.Construct, id: string, props: HitCounterProps) {
      super(scope, id);

      const table = new dynamodb.Table(this, "Hits", { // lints `this`
         partitionKey : { 
            name : "path", 
            type : dynamodb.AttributeType.STRING 
         }
      });
      <more code removed>
   }
}
like image 262
Sean D Avatar asked Dec 01 '22 13:12

Sean D


2 Answers

I had the same issue. You have to set all the different cdk packages to the same version, see https://github.com/aws/aws-cdk/issues/542#issuecomment-449694450.

like image 124
Richard Avatar answered Dec 04 '22 08:12

Richard


  1. cd to your project path
  2. run: npx npm-check-updates -u
  3. run: npm install
  4. restart your IDE
like image 34
Jorge López Avatar answered Dec 04 '22 08:12

Jorge López