0

I have the following code,

const userCreator = (name: string) => {
    const user = Object.create(addedFunc)
    user.name = name;
    user.counter = 1;
    return user;
}

const addedFunc = {
    increment: function() { this.counter++ }
}

const user1 = userCreator('Yoda') 
user1.increment();


console.log(user1); // {name: Yoda, counter: 2}

I am aware this is not production code, would normally use classes, for this. But for me this is a mental exercise in understanding the prototype chain, and how using Object.create to place the function increment on the Object and then javascript going looking for it. This code works as expected but typescript complains that increment function cannot be found on user. Is there a way to reasonably silence this error or how do I type increment to stop complaining?

Here is exact error:

enter image description here

Faktor 10
  • 1,868
  • 2
  • 21
  • 29

0 Answers0