【问题标题】:Avoid no-unused-expession warning in TypeScript using "&&"-operator使用 \"&&\"-operator 在 TypeScript 中避免 no-unused-expession 警告
【发布时间】:2023-01-25 18:25:03
【问题描述】:

如果我在 TypeScript 中执行以下操作,编译器将抱怨“no-unused-expession”规则:

!hasSelectedValues && this.loadOptions([]);

我怎样才能让 TS 明白这个表达式 (hasSelectedValues) 不是“未使用的”?我愿意不是想要完全删除未使用表达式的规则。

【问题讨论】:

    标签: typescript


    【解决方案1】:

    为避免遇到该 TypeScript 错误,请不要使用 && 作为副作用,使用 if

    if (!hasSelectedValues) {
        this.loadOptions([]);
    }
    

    或者如果你真的想保持简洁:

    if (!hasSelectedValues) this.loadOptions([]);
    

    【讨论】:

    • 行。但是你为什么不使用这个呢?
    猜你喜欢
    • 2021-02-13
    • 2020-01-25
    • 1970-01-01
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 2020-04-09
    相关资源
    最近更新 更多