如何禁用 tslint 文件中的规则“TS2322:类型 'WebSocketAction' 不可分配给类型 'boolean'”

问题描述

有人可以帮我在 tslint 文件中禁用此规则吗? 我有这样的消息:

“TS2322:“WebSocketAction”类型不能分配给“boolean”类型。”

export class WebSocketData {

  public authorization: string;

  constructor(
    public action: WebSocketAction = null,public data: any = null,token: string = null
  ) {
    this.authorization = token ? `Bearer ${token}` : null;
  }

  public isValid(): boolean {
    return this.data && this.action;
  }
}

screenshot

解决方法

&& 运算符需要 boolean,但 this.actionWebSocketAction,因此您会收到打字稿错误。

您可以使用双感叹号 !! 解决错误:

  public isValid(): boolean {
    return this.data && !!this.action;
  }