首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >此条件将始终返回'false‘,因为'string[]’和'string‘类型没有重叠。ts(2367)

此条件将始终返回'false‘,因为'string[]’和'string‘类型没有重叠。ts(2367)
EN

Stack Overflow用户
提问于 2021-12-23 09:59:51
回答 1查看 7.6K关注 0票数 1

今天我在玩角游戏,这时出现了这个错误。我在Typescript error This condition will always return 'true' since the types have no overlap上读到了公认的答案,但我不知道它意味着什么。

模板:

代码语言:javascript
运行
复制
<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>

构成部分:

代码语言:javascript
运行
复制
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
    }
  }

}

错误:

代码语言:javascript
运行
复制
This condition will always return \'false\' since the types \'string\[\]\' and \'string\' have no overlap. ts(2367)
EN

Stack Overflow用户

回答已采纳

发布于 2021-12-23 10:38:54

您自己将counts定义为string[],然后尝试将其与空字符串进行比较。字符串数组永远不等于任何字符串。

我不知道你觉得哪部分令人困惑。错误再清楚不过了:您正在尝试比较字符串数组和字符串。这两者永远不会相等,所以条件counts === ""总是要计算为false,这可能是一个错误,所以TypeScript是正确地告诉你要更多的注意。编写条件if (条件总是false )是没有意义的--代码永远不会运行,因此您要么需要删除整个块,要么需要更改条件。

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70460464

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档