问题描述
有人可以帮我在 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;
}
}
解决方法
&&
运算符需要 boolean
,但 this.action
是 WebSocketAction
,因此您会收到打字稿错误。
您可以使用双感叹号 !!
解决错误:
public isValid(): boolean {
return this.data && !!this.action;
}