首页 > 解决方案 > 打字稿不理解松散平等和严格平等吗?

问题描述

我收到的错误:

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

代码:

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

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

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

它不会总是返回 true

标签: typescript

解决方案


我想你要澄清为什么打字稿显示错误(来自打字稿显示错误消息)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)

推荐阅读