Interface 可以在接口定义中使用getter/setter吗?

Interface 可以在接口定义中使用getter/setter吗?,interface,get,set,accessor,typescript,Interface,Get,Set,Accessor,Typescript,目前,TypeScript不允许在接口中使用get/set方法(访问器)。 例如: interface I { get name():string; } class C implements I { get name():string { return null; } } 此外,TypeScript不允许在类方法中使用数组函数表达式: 例如: 有没有其他方法可以在接口定义上使用getter和setter?首先,Typescript在针

目前,
TypeScript
不允许在接口中使用get/set方法(访问器)。 例如:

interface I {
      get name():string;
}

class C implements I {
      get name():string {
          return null;
      } 
}
此外,TypeScript不允许在类方法中使用数组函数表达式: 例如:


有没有其他方法可以在接口定义上使用getter和setter?

首先,Typescript在针对Ecmascript 5时仅支持
get
set
语法。要实现这一点,必须使用

tsc --target ES5
接口不支持getter和setter。要让代码编译,您必须将其更改为

interface I { 
    getName():string;
}

class C implements I { 
    getName():string {
          return null;
    }   
}
typescript支持的是构造函数中字段的特殊语法。就你而言,你本可以

interface I {
    getName():string;
}

class C implements I {
    constructor(public name: string) {
    }
    getName():string {
        return name;
    }
}
注意class
C
如何不指定字段
name
。它实际上是在构造函数中使用语法sugar
public-name:string
声明的


正如Sohnee指出的,接口实际上应该隐藏任何实现细节。在我的示例中,我选择了需要java风格getter方法的接口。但是,您也可以创建一个属性,然后让类决定如何实现接口。

您可以在接口上指定属性,但不能强制是否使用getter和setter,如下所示:

interface IExample {
    Name: string;
}

class Example implements IExample {
    private _name: string = "Bob";

    public get Name() {
        return this._name;
    }

    public set Name(value) {
        this._name = value;
    }
}

var example = new Example();
alert(example.Name);
在本例中,接口不强制类使用getter和setter,我可以使用属性来代替(下面的示例)-但是接口应该隐藏这些实现细节,因为它向调用代码承诺它可以调用什么

interface IExample {
    Name: string;
}

class Example implements IExample {
    // this satisfies the interface just the same
    public Name: string = "Bob";
}

var example = new Example();
alert(example.Name);
最后,类方法不允许使用
=>
——如果您认为它有一个火爆的用例,您可以这样做。以下是一个例子:

class Test {
    // Yes
    getName = () => 'Steve';

    // No
    getName() => 'Steve';

    // No
    get name() => 'Steve';
}

为了补充其他答案,如果您希望在界面上定义
get值
,可以使用
readonly

interface Foo {
  readonly value: number;
}

let foo: Foo = { value: 10 };

foo.value = 20; //error

class Bar implements Foo {
  get value() {
    return 10;
  }
}
但就我所知,正如其他人所提到的,目前没有办法在接口中定义一个仅限集合的属性。但是,您可以将限制移到运行时错误(仅在开发周期中有用):

不推荐的做法;但这是一种选择

使用TypeScript 3.4:

接口IPart{
getQuantity():数字;
}
类部件实现IPart{
私人数量:数量;
建造师(数量:个){
这个。数量=数量;
}
public getQuantity=():number=>{
退回这个数量;
};
}
let零件=新零件(42);
//在typescript中使用时,无法访问数量。
//然而,当编译成javascript时,它将记录“42”。
控制台日志(部件数量);
//日志'42'。
console.log(part.getQuantity());

参见上的示例。

您可以在TypeScript中使用
get
set
关键字。ECMAScript 5支持对象的旁注。IE8+、FF4+、Opera 12+、WebKit和Safari支持defineProperty。还有一个EC5垫片,您可以使用
=>
来定义这样的类方法:
name=(a:string)=>this.\u name但在输出JS中,它将在类函数中定义,而不是扩展其原型对象。这似乎不适用于静态get属性:/根据最新的typescript版本4.3.2,这个答案是正确的。不完全是OP要求的,而是+1,用于显示接口如何在实现该接口的类中强制定义方法。
interface Foo {
  readonly value: number;
}

let foo: Foo = { value: 10 };

foo.value = 20; //error

class Bar implements Foo {
  get value() {
    return 10;
  }
}
interface Foo {
  /* Set Only! */
  value: number;
}

class Bar implements Foo {
  _value:number;
  set value(value: number) {
    this._value = value;
  }
  get value() {
    throw Error("Not Supported Exception");
  }
}