typescript - 打字稿不理解松散平等和严格平等吗?
问题描述
我收到的错误:
此条件将始终返回 'true',因为类型 'string' 和 'number' 没有重叠。
代码:
if (this.$route.params.groupId != this.group.id) {
more code here
}
'1' != 1(将返回 false)
'1' !== 1 (将返回真)
它不会总是返回 true
解决方案
我想你要澄清为什么打字稿显示错误(来自打字稿显示错误消息)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)
推荐阅读
- ios - Swift:添加项目时更改 UICollectionView 中单元格布局的宽度
- neo4j - 删除 neo4j 中的 MIN 计数数据
- java - 反应式 - 混合阻塞和异步请求
- c# - 如何等待场景卸载?
- angular - 为什么 HTTP 请求以代码 500 完成时 *finalize* 运算符不起作用
- jestjs - 在单个测试套件中独立运行多个测试用例,无需重新启动应用程序
- r - 重新排列数据表中的列会影响 R 中的其他变量
- c++ - “删除”的转义序列
- json - 如何在可编码解码过程中处理空数组
- javascript - 我想更改页面背景颜色,但在 Chrome 中无法正常工作