P
P
partyzanx2022-03-27 16:39:17
typescript
partyzanx, 2022-03-27 16:39:17

How to type new functions?



Link to the sandbox

interface IPerson {
    name: string;
    sayHello(): string;
}

const Welcome = function (this: IPerson, name: string) {
  this.name = name;
}  

Welcome.prototype.sayHello = function () {
  return "Hello, " + this.name + "!";
};

interface PersonConstructor {
    new(name: string): IPerson;
}

var welcome = new Welcome("John");
console.log(welcome.sayHello());


Throws an error 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.(7009)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WbICHA, 2022-03-28
@partyzanx

const Welcome = function(this: IPerson,name: string) {
    this.name=name;
} as unknown as PersonConstructor

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question