TypeScript 中的函数重载错误:访问者模式

问题描述

我正在尝试创建一个访问者设计模式,但我遇到了一个编译错误,尽管我花了好几个小时试图找到一个解决方案,但我还是无法解决这个错误......

访客界面:

export interface Visitor {
    visit(a: A): X;
    visit(b: B): Y;
}

访客实现:

export class VisitorImp implements Visitor {

    visit(a: A): X {
      return a.getX();
    }

    visit(b: B): Y{
     return b.getY();
    }
}

有了这个,我有以下编译错误:

Property 'visit' in type 'VisitorImp' is not assignable to the same property in base type 'Visitor'.
  Type '(a: A) => X' is not assignable to type '{ (a: A): X; (b: B): Y; }'.
    Type 'X' is missing the following properties from type Y

我真的希望有人能帮助我,因为它现在让我发疯!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)