今天我在玩角游戏,这时出现了这个错误。我在Typescript error This condition will always return 'true' since the types have no overlap上读到了公认的答案,但我不知道它意味着什么。
模板:
<div class="container">
Paste your text here: <textarea #count width="150" height="150"></textarea><br>
<button class="btn btn-dark" (click)=check(count.value)>Count</button>
<br>
<p id="result"></p>
</div>
构成部分:
import { Component, OnInit } from '@angular/core';
// import { checkServerIdentity } from 'tls'; i don't exist!
@Component({
selector: 'app-main',
templateUrl: './main.component.html',
styleUrls: ['./main.component.css']
})
export class MainComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
check(text:string) {
var counts:string[];
counts = text.split(" ");
let resultid = document.getElementById("result");
if (counts === "" /* here error */) {
// where i left over
}
}
}
错误:
This condition will always return \'false\' since the types \'string\[\]\' and \'string\' have no overlap. ts(2367)
发布于 2021-12-23 10:38:54
您自己将counts
定义为string[]
,然后尝试将其与空字符串进行比较。字符串数组永远不等于任何字符串。
我不知道你觉得哪部分令人困惑。错误再清楚不过了:您正在尝试比较字符串数组和字符串。这两者永远不会相等,所以条件counts === ""
总是要计算为false
,这可能是一个错误,所以TypeScript是正确地告诉你要更多的注意。编写条件if
(条件总是false
)是没有意义的--代码永远不会运行,因此您要么需要删除整个块,要么需要更改条件。
https://stackoverflow.com/questions/70460464
复制相似问题