简体   繁体   中英

how to inheritance typescript class from javascript class

I use the Javascript module to extending Class for Custom extended Class.

I wrote my Custom Class in typescript, And I met an error below messages.

Property 'jsFunc' does not exist on type 'tsClass'.ts(2339)

I think Because Javascript Class have no type information, So It can't bring any properties.

how to correctly work on this problem.

example

book.js

class book {
  page;
  constructor(page) {
    this.page = page;
  }

  open() {
    _next();
  }

  _next() {
    this.page = this.page++;
  }
}

comicbook.ts

class commicbook extends book {
  page; // if it isn't It would be error that does not exist
  open() {
    this.page = 10;
    _next(); // Property '_next()' does not exist on type 'commicbook'.ts
  }
}

To call a method on your current class, you need to prepend this :

this._next();

One way to inherit typescript class from javascript class would be:

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}`;
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM