Typescript Class Constructor Self Initialization | Task

Ole Ersoy
Oct - 09  -  1 min

Scenario

We have the following object:

const c = {
   members: ['1', '2']
}

And we want to make it an instance of the class:

class Members {
    members: string[];
    log() {
    this.members.forEach(m=>{
        console.log(m);
    })
  }
}

Can we just do:

const c2:Members = c;

?

Answer

We can’t do this because the class Members contains the method log().

If we remove that function from the class then this work:

const c2:Members = c;

Use this technique when the Class contains functions