如何从javascript类继承typescript类
我使用Javascript模块为自定义扩展类扩展类 我用typescript编写了自定义类,但在消息下面遇到了一个错误如何从javascript类继承typescript类,javascript,typescript,Javascript,Typescript,我使用Javascript模块为自定义扩展类扩展类 我用typescript编写了自定义类,但在消息下面遇到了一个错误 Property 'jsFunc' does not exist on type 'tsClass'.ts(2339) 我认为,因为Javascript类没有类型信息,所以它不能带来任何属性 如何正确处理这个问题 范例 book.js 教材{ 页 建造师(第页){ this.page=page; } 开(){ _next(); } _下一个(){ this.page=this
Property 'jsFunc' does not exist on type 'tsClass'.ts(2339)
我认为,因为Javascript类没有类型信息,所以它不能带来任何属性
如何正确处理这个问题
范例
book.js
教材{
页
建造师(第页){
this.page=page;
}
开(){
_next();
}
_下一个(){
this.page=this.page++;
}
}
漫画书
class commicbook扩展了书本{
page;//如果不是,则是不存在的错误
开(){
本页=10;
_next();//类型“commicbook.ts”上不存在属性“_next()”
}
}
要在当前类上调用方法,您需要在前面加上以下内容:
this._next();
要在当前类上调用方法,您需要在this
前面加上前缀:
this._next();
从javascript类继承typescript类的一种方法是:
class Person {
firstName: string;
lastName: string;
constructor (fName: string, lName: string) {
this.firstName = fName;
this.lastName = lName;
}
getFullName() {
return `${firstName} ${lastName}`;
}
}
class Employee extends Person {
empID: string;
designation: string;
constructor (fName: string, lName: string,
eID: string, desig: string) {
super(fName, lName);
this.empID = eID;
this.designation = desig;
}
toString() {
return `${empID} - ${firstName} ${lastName}
=> ${designation}`;
}
}
从javascript类继承typescript类的一种方法是:
class Person {
firstName: string;
lastName: string;
constructor (fName: string, lName: string) {
this.firstName = fName;
this.lastName = lName;
}
getFullName() {
return `${firstName} ${lastName}`;
}
}
class Employee extends Person {
empID: string;
designation: string;
constructor (fName: string, lName: string,
eID: string, desig: string) {
super(fName, lName);
this.empID = eID;
this.designation = desig;
}
toString() {
return `${empID} - ${firstName} ${lastName}
=> ${designation}`;
}
}