i'm trying to mix javascript prototype with class based, like this:
function Cat(name) {
this.name = name;
}
Cat.prototype.purr = function(){
console.log(`${this.name} purr`);
}
Cat.prototype.meow = function(){
console.log(`${this.name} meow`);
}
class Tiger extends Cat {
constructor(name) {
super(name);
}
meow() {
console.log(`${this.name} roar`);
}
}
the JS code above is valid. then I convert the code to typescript like this:
function Cat(this : any, name : string) {
this.name = name;
}
Cat.prototype.purr = function(){
console.log(`${this.name} purr`);
}
Cat.prototype.meow = function(){
console.log(`${this.name} meow`);
}
// error: Type '(this: any, name: string) => void' is not a constructor function type.
class Tiger extends Cat {
constructor(name : string) {
super(name);
}
meow() {
console.log(`${this.name} roar`);
}
}
the class Tiger doesn't accept the class Cat as its base class. (valid in JS but not in TS). I cannot change the Cat to standard class syntax because I need the .prototype access to be pointed to another JS library.
anyone can fix the TS code above? maybe add some .d.ts definition.
Note: adding // @ts-ignore works but I cannot do that because the VS-code intellisense will not work.
classsyntax uses prototype objects in exactly the same way. Please be more specific about that constraint - you really should useclass Cat { … }in TypeScript.