Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare Enum Angular 4

Tags:

angular

oportunidad.model.ts:

const enum TipoSiNo {
SI, NO
}


export class Oportunidad {
constructor(
  public resultadoValidacion?: TipoSiNo,

I need to buy the value of my object with one of the possible values of the enumerated:

oportunidad-edit.component.ts:

add:

const enum TipoSiNo {
SI, NO
}

this.oportunidad.resultadoValidacion === TipoSiNo.NO

Give me the following error

ERROR in [at-loader] ./src/main/webapp/app/entities/oportunidad/oportunidad-edit.component.ts:337:75 
TS2365: Operator '===' cannot be applied to types 'TipoSiNo.NO' and 'TipoSiNo.NO'.
like image 984
Jose Avatar asked Mar 09 '26 04:03

Jose


2 Answers

I can see you are creating the same enum: TipoSiNo at two different places and hence both are two different types so the comparison is not going to work.

I came here to find why I can't compare the two enum values from the same enum, but couldn't really get any answer anywhere. Example:

export enum Stage{
NotStarted, QuarterOf, Half, QuarterTo, Full
}

export class Work{
id: number,
title: string,
description: string,
status: Stage
}

// check if work is not started
if(this.currentWork===Stage.NotStarted){ // <- this is never true
   // do something
}

So, I found a workaround:

// check if work is not started
if(this.currentWork.toString===Stage[Stage.NotStarted]){ // <- this works
   // do something
}

Hopefully, it helps someone

like image 162
Anup Sharma Avatar answered Mar 11 '26 20:03

Anup Sharma


Try this:

this.oportunidad.resultadoValidacion as TipoSiNo === TipoSiNo.NO as TipoSiNo
like image 37
subaru710 Avatar answered Mar 11 '26 20:03

subaru710



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!