【发布时间】:2018-04-22 14:04:30
【问题描述】:
我有一个 Exception1 类,它扩展了 JavaScript 的 Error 类,同时它还实现了一个接口 SampleInterface
interface SampleInterface {
func(): string;
}
class SampleClass {
public message: string;
constructor(message: string) {
this.message = message;
}
}
class Exception1 extends Error implements SampleInterface {
constructor(message: string) {
super(message);
}
public func(): string {
return "Exception1"
}
}
在控制台中执行console.log(new Exception1('a').func()) 时出现此错误
Uncaught TypeError: (intermediate value).func is not a function
at <anonymous>:1:33
但是,例如,如果该类扩展了其他一些类,这将按预期工作
class Exception2 extends SampleClass implements SampleInterface {
constructor(message: string) {
super(message);
}
public func(): string {
return "Exception2"
}
}
在执行 console.log(new Exception2('a').func()) 时,我得到了预期的输出 Exception2
【问题讨论】:
-
好的,这解决了问题,把它作为答案,我会标记为接受。谢谢
标签: javascript typescript