【问题标题】:Does typescript not understand loose equality and strict equality?打字稿不理解松散平等和严格平等吗?
【发布时间】:2021-09-16 18:55:17
【问题描述】:

我收到的错误:

此条件将始终返回 'true',因为类型 'string' 和 'number' 没有重叠。

代码:

if (this.$route.params.groupId != this.group.id) {
      more code here
 }

'1' != 1(将返回 false)

'1' !== 1(将返回 true)

它不会总是返回 true

【问题讨论】:

标签: typescript


【解决方案1】:

我想你澄清一下为什么打字稿显示错误(来自打字稿显示错误消息)https://github.com/microsoft/TypeScript/issues/26592

在我看来,我们可以确定变量的类型以正确比较它

use `===` or `!==` to compare value
Determine of type variable
const routeGroupId: string = this.$route.params.groupId;
const currentGroupId: string = this.group.id;
if(routeGroupId != currentGroupId)

【讨论】:

    猜你喜欢
    • 2016-04-24
    • 2020-07-24
    • 2018-06-08
    • 1970-01-01
    • 2012-02-12
    • 2012-02-10
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    相关资源
    最近更新 更多