类型Array的Typescript错误: - 无法调用类型缺少调用签名的表达式

时间:2018-06-01 03:58:56

标签: javascript typescript angular5

我在Angular5中使用typescript创建了getter-setter: -

  private _locations: Array<string> = [];

  constructor() { }

  /**
   * Getter locations
   * @return {Array<string>}
   */
  public get locations(): Array<string> {
    return this._locations;
  }

  /**
   * Setter locations
   * @param {Array<string>} value
   */
  public set locations(value: Array<string>) {
    this._locations = value;
  }

当我尝试访问方法位置时

this.signUpService.locations();

我收到错误:[ts] Cannot invoke an expression whose type lacks a call signature. Type 'string[]' has no compatible call signatures.

1 个答案:

答案 0 :(得分:8)

  

this.signUpService.locations();

getters 未被调用。他们只是得到

修正:

const value = this.signUpService.locations; 

This is just TypeScript saving you