Typescript子类函数重载

时间:2016-06-25 05:38:20

标签: inheritance typescript overloading

如何在打字稿中实现与此模式类似的功能?

class A {
    Init(param1: number) {
        // some code
    }
}

class B extends A {
    Init(param1: number, param2: string) {
        // some more code
    }
}

上面提到的代码似乎应该可以正常工作,但是仔细检查How Typescript function overloading works就会发生错误:

TS2415: 'Class 'B' incorrectly extends base class 'A'. 
Types of property 'Init' are incompatible.

我知道构造函数允许这种行为,但是我不能在这里使用构造函数,因为这些对象被用于内存效率。

我可以在A类中提供另一个Init()定义:

class A {
    Init(param1: number, param2: string): void;
    Init(param1: number) {
        // some code
    }
}

然而,这不太理想,因为现在基类需要知道它的所有派生类。

第三个选项是重命名B类中的Init方法,但这不仅非常丑陋和令人困惑,而是在基类中暴露出Init()方法,这会导致难以检测的错误错误地调用基类Init()。

有没有办法实现这种没有上述方法缺陷的模式?

2 个答案:

答案 0 :(得分:4)

TypeScript抱怨方法不可互换:如果执行以下操作会发生什么?

let a:A = new A(); // a is of type A
a.Init(1)
a = new B(); // a is still of type A, even if it contains B inside
a.Init(1) // second parameter is missing for B, but totally valid for A, will it explode?

如果您不需要它们可以互换,请修改B的签名以符合A的条件:

class B extends A {
    Init(param1: number, param2?: string) { // param 2 is optional
        // some more code
    }
}

但是,您可能会发现自己需要创建一个具有完全不同方法签名的类:

class C extends A {
    Init(param1: string) { // param 1 is now string instead of number
        // some more code
    }
}

在这种情况下,添加一个满足当前类和基类调用的方法签名列表。

class C extends A {
    Init(param1: number)
    Init(param1: string)
    Init(param1: number | string) { // param 1 is now of type number | string (you can also use <any>)
        if (typeof param1 === "string") { // param 1 is now guaranteed to be string
            // some more code
        }
    }
}

这样A类就不必了解任何派生类。作为权衡,您需要指定满足基类和子类方法调用的签名列表。

答案 1 :(得分:0)

对于想要扩展类型的人。 基于zlumer's answer,并使用Intersection types

interface ConsumerGroup {
  on(message: 'message'): void
  on(message: 'error'): void
}

interface ConsumerGroup2 {
  on(message: 'messageDecoded'): void;
  on(message: 'rawMessage'): void;
}

// Intersection types
type ConsumerGroupEx = ConsumerGroup2 & ConsumerGroup;

function newEvent(): ConsumerGroupEx {
  return "just for test" as unknown as ConsumerGroupEx;
}

const evt = newEvent();

evt.on('messageDecoded'); // ok
evt.on('message'); // ok
evt.on('error'); // ok
evt.on('notExist'); // compilation error