小编mur*_*adm的帖子

Typescript:具有按名称 getter / setter 属性的类

假设有一个接口:

interface ServiceDataIf {
  somethingToDo(): void;
  mayBeAdd(arg: any): void;
  mayBeGet(name: string): any;
  readonly someVal: string;
  anotherVal: string;
  [name: string]: any;
}
Run Code Online (Sandbox Code Playgroud)

如何在类中实现该接口:

class ServiceDataImpl1 implements ServiceDataIf {
  // easy with properties
  get someVal(): string {
    const result = /* somehow get it */;
    return result;
  }

  constructor() {}

  set anotherVal(v: string): void {
    // remember somewhere v
  }

  // plain methods easy as well
  somethingToDo(): void { /* do something */ }
  mayBeAdd(arg: any): void { /* do another something …
Run Code Online (Sandbox Code Playgroud)

typescript

5
推荐指数
1
解决办法
4395
查看次数

标签 统计

typescript ×1