我有以下内容:
interface IEngine {
type():string;
type(type:string):void;
}
class Engine implements IEngine {
private _type: string;
get type():string {
return this._type;
}
set type(type:string) {
this._type = type;
}
}
var engine = new Engine();
engine.type = 'foo';
接口(interface)在我看来已经实现,但是,运行 tsc 会抛出异常:
F:\>tsc interfaces.ts --target "es5"
interfaces.ts(11,7): error TS2420: Class 'Engine' incorrectly implements interface 'IEngine'.
Types of property 'type' are incompatible.
Type 'string' is not assignable to type '{ (): string; (type: string): void; }'.
最佳答案
你正在实现属性,所以在接口(interface)中它应该是这样的:
interface IEngine {
type:string;
}
关于javascript - 接口(interface)和 Getters\Setters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36701827/