小编Lee*_*e H的帖子

如何在TypeScript中的派生类中进行构造函数重载?

假设我有一个'基础'类,如下所示:

class CcDefinition {
  // Some properties here

  constructor (json: string);
  constructor (someVar: number, someOtherVar: string);
  constructor (jsonOrSomeVar: any, someOtherVar?: string) {
    if (typeof jsonOrSomeVar=== "string") {
      // some JSON wrangling code here
    } else {
      // assign someVar and someOtherVar to the properties
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我希望能够扩展这个基类,同时仍然支持构造函数重载.例如:

class CcDerived extends CcDefinition {
  // Some additional properties here

  constructor (json: string);
  constructor (someVar: boolean, someOtherVar: number, someAdditionalVar: string);
  constructor (jsonOrSomeVar: any, someOtherVar?: number, someAdditionalVar?: string) {
    if (typeof jsonOrSomeVar=== "string") …
Run Code Online (Sandbox Code Playgroud)

inheritance constructor extends overloading typescript

8
推荐指数
1
解决办法
1万
查看次数