gpt4 book ai didi

javascript - 接口(interface)和 Getters\Setters

转载 作者:搜寻专家 更新时间:2023-10-30 22:04:52 24 4
gpt4 key购买 nike

我有以下内容:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com