是否可以在 TypeScript 接口中使用 getter/setter?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12844893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:52:22  来源:igfitidea点击:

Is it possible to use getters/setters in TypeScript Interfaces?

typescript

提问by Ezward

I would like to define an interface with a readonly property. For instance;

我想定义一个具有只读属性的接口。例如;

interface foo {
    get bar():bool;
}

However, this gives the syntax error, "expected ';'" on bar. I have setup my VisualStudio to use the ES5 target, so getters are supported. Is this a limitation of interfaces? Might this change in the future; it is a very nice thing to be able to do.

但是,这会导致语法错误,“预期为 ';'”。我已将 VisualStudio 设置为使用 ES5 目标,因此支持 getter。这是接口的限制吗?未来可能会发生这种变化;能够做到这一点是一件非常好的事情。

回答by Vitaliy Ulantikov

Getter-only properties were introduced in Typescript 2.0:

Typescript 2.0中引入了 Getter-only 属性:

interface foo {
    readonly bar: boolean;
}

回答by Valentin

Yes, this is a limitation of interfaces. Whether or not the access to the property is implemented with a getter is an implementation detail and thus should not be part of the public interface. See also this question.

是的,这是接口的限制。是否使用 getter 实现对属性的访问是一个实现细节,因此不应成为公共接口的一部分。另请参阅此问题

If you need a readonly attribute specified in an interface, you can add a getter method:

如果需要在接口中指定 readonly 属性,可以添加 getter 方法:

interface foo {
    getAttribute() : string;
}

回答by chharvey

As @Vitaliy Ulantikov answered, you may use the readonlymodifier on a property. This acts exactly like a getter.

正如@Vitaliy Ulantikov 所回答的那样,您可以readonly在属性上使用修饰符。这就像一个吸气剂。

interface Point {
    readonly x: number;
    readonly y: number;
}

When an object literalimplements the interface, you cannot overwrite a readonlyproperty:

对象字面量实现接口时,您不能覆盖readonly属性:

let p1: Point = { x: 10, y: 20 };
p1.x = 5; // error!

But when a classimplements the interface, there is no way to avoid overwriting it.

但是当一个实现了接口时,就没有办法避免覆盖它。

class PointClassBroken implements Point {
    // these are required in order to implement correctly
    x: number;
    y: number;

    constructor(x: number, y: number) {
        this.x = x
        this.y = y
    }

    changeCoordinates(x: number, y: number): void {
        this.x = x // no error!
        this.y = y // no error!
    }
}

I guess that's because when you re-declare properties in the class definition, they override the properties of the interface, and are no longer readonly.

我猜这是因为当您在类定义中重新声明属性时,它们会覆盖接口的属性,并且不再是只读的。

To fix that, use readonlyon the properties directly in the class that implements the interface

要解决这个问题,请readonly直接在实现接口的类中使用属性

class PointClassFixed implements Point {
    readonly x: number;
    readonly y: number;

    constructor(x: number, y: number) {
        this.x = x
        this.y = y
    }

    changeCoordinates(x: number, y: number): void {
        this.x = x // error!
        this.y = y // error!
    }
}

See for yourself in the playground.

操场上亲眼看看。