Typescript错误,因为类型没有重叠,所以此条件将始终返回“ true”

ali*_*990 8 javascript typescript typescript2.0 angular

我在表单组上有此条件:

if((age>17 && (this.frType=="Infant")) 
|| (age>40 && this.frType=="Grandchild")
|| (age<=5 && 
   (this.frType!="Child" 
   || this.frType!="Infant" 
   || this.frType!="Grandchild" || this.frType!="Cousin")))
Run Code Online (Sandbox Code Playgroud)

它包含3个主要条件:

  1. 如果年龄17岁,则无法设置为 infant
  2. 如果一个人大于40岁,他就不能成为 grandchild
  3. 如果一个人是不到5年时间,他应该是childinfantgrandchildcousin

如果这些条件之一为真,我将发送一条错误消息。

我收到的错误是:

[ts]由于类型'“ Child”'和'“ Infant”'没有重叠,因此此条件将始终返回'true'。[2367]

在这部分if条件上:

|| this.frType!="Infant" || this.frType!="Grandchild" || this.frType!="Cousin")))
Run Code Online (Sandbox Code Playgroud)

我在其他组件中使用了确切的条件,并且没有显示错误。

if((age>17 && (this.family_relation_type=="Infant")) 
|| (age>40 && this.family_relation_type=="Grandchild")
|| (age<=5 && 
   (this.family_relation_type!="Child" || 
    this.family_relation_type!="Infant" || 
    this.family_relation_type!="Grandchild" || 
    this.family_relation_type!="Cousin")))
Run Code Online (Sandbox Code Playgroud)

这是我在这两个部分中计算年龄的方式:

let timeDiff = Math.abs(Date.now() - this.formGroup.controls['dob'].value);
let age = Math.floor((timeDiff / (1000 * 3600 * 24))/365);
Run Code Online (Sandbox Code Playgroud)

Álv*_*tín 16

也许我可以帮助某人解决这个问题。

就我而言,错误是由以下原因触发的:

*ngIf="fooArray.length === 0"

所以我将其修改为:

*ngIf="fooArray.length < 1"

对我来说毫无意义,但它有效。

  • 我知道这不是 TS 问题的一部分,我只是想帮助有相同情况的人。 (5认同)
  • 最好忽略 Angular `*ngIf` 的东西,它只是与当前的 TypeScript 问题无关的噪音。 (3认同)

jaf*_*mlp 10

我最近为这个问题苦苦挣扎。在这里分享我的经验

基本上 IDE 不允许将 object.enum 与字符串进行比较。作为解决方案,在component.ts中添加一个方法来比较枚举

细节 :

export enum Status {
     NEW,
     PROGRESS,
     FINISHED
}

export interface Model {
   id : number;
   name : string;
   status : Status
}
Run Code Online (Sandbox Code Playgroud)

现在在 component.html 中,我试图比较模型状态

<div *ngFor="let m of modelItems" >
      <i *ngIf="m.status === 'NEW'" class="icon-new"></i>
</div>

Error : This condition will always return 'false' since the types 'Status' and 'string' have no overlap.ngtsc(2367)
Run Code Online (Sandbox Code Playgroud)

我还尝试在 component.ts 中定义状态枚举并使用它进行比较

public StatusEnum = Status; 

<div *ngFor="let m of modelItems" >
      <i *ngIf="StatusEnum[m.status] === 'NEW'" 
        class="icon-new"></i>
</div>
Run Code Online (Sandbox Code Playgroud)

使用上述解决方案,不会出现 IDE 错误,但条件永远不会为真,因为 enum[value] 给出一个数值。

我尝试的下一个选项如下

<div *ngFor="let m of modelItems" >
      <i *ngIf="m.status=== StatusEnum[StatusEnum.NEW]" class="icon-new"></i>
    </div>
Run Code Online (Sandbox Code Playgroud)

但是在IDE中又报错了

Error : This condition will always return 'false' since the types 'Status' and 'string' have no overlap.ngtsc(2367)
Run Code Online (Sandbox Code Playgroud)

最后是什么解决了这个问题,它在 component.ts 中实现了一个方法

解决方案

组件.ts

public StatusEnum = Status; //To refer in the HTML

 checkStatus(m: Model, status: Status): boolean {
    return Status[m.status] as unknown === status;
  } 
Run Code Online (Sandbox Code Playgroud)

注意:状态[m.status]未知

超文本标记语言

<div *ngFor="let m of modelItems" >
       <i *ngIf="checkStatus(m,StatusEnum.NEW)" 
       class="icon-new"></i>
  </div>    
Run Code Online (Sandbox Code Playgroud)


Cer*_*nce 7

考虑独立表达式:

(this.frType!="Child" || this.frType!="Infant")
Run Code Online (Sandbox Code Playgroud)

如果frTypeChild,则第二部分为true,因此表达式的计算结果为true。如果frTypeInfant,则第一部分为true,因此表达式的计算结果为true。如果既不frType也不 ChildInfant,则第一部分将为true,并且表达式将再次评估为true-逻辑错误,它将始终解析为true

(如果您||Grandchild和添加其他条件Cousin,则同样的事情不断发生-它始终会解析为true

&&改用:

|| (age<=5 && (
   this.frType!="Child" 
   && this.frType!="Infant" 
   && this.frType!="Grandchild"
   && this.frType!="Cousin"
 ))
Run Code Online (Sandbox Code Playgroud)

或者,为了使逻辑更容易理解,您可以考虑使用数组,并使用.includes

const kidsFiveAndUnder = ['Child', 'Infant', 'Grandchild', 'Cousin'];
// ...
|| (age <= 5 && !kidsFiveAndUnder.includes(this.frType))
Run Code Online (Sandbox Code Playgroud)


Moh*_*man 6

明确定义所有变量的数据类型。

例如,此代码具有线程标题中提到的相同错误,我通过显式定义变量的数据类型来修复。

从:

const selectedLangCulture = "en"; // or "ar-SA"
const direction = "rtl";
const languageChanged =
  (direction === "rtl" && selectedLangCulture === "en") ||
  (direction === "ltr" && selectedLangCulture === "ar-SA");
Run Code Online (Sandbox Code Playgroud)

到:

const selectedLangCulture: string = "en"; // Put the datatype string.
const direction: string = "rtl"; // Put the datatype string.
const languageChanged =
  (direction === "rtl" && selectedLangCulture === "en") ||
  (direction === "ltr" && selectedLangCulture === "ar-SA");
Run Code Online (Sandbox Code Playgroud)