Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"have no overlap" error while comparing enum values in TypeScript

Tags:

typescript

I'm trying to learn TypeScript and had been following online tutorial examples for enum support in TypeScript. For this below snippet:

enum daysoftheweek{
    SUN, MON, TUE, WED, THU, FRI, SAT
}

let day:daysoftheweek ;

day = daysoftheweek.FRI; //line 7

if (day === daysoftheweek.MON){
    console.log("got to go to work early");
}else{
    console.log("I may go late");
}

...I'm getting this error at compile time and I don't understand why:

TS2367: This condition will always return 'false' since the types 'daysoftheweek.FRI' and 'daysoftheweek.MON' have no overlap.

If I modify line 7 to this, the error goes off: day = daysoftheweek.MON;

Can somebody please explain why compilation is throwing that error?
(I followed other threads on this "have no overlap" error , but couldn't understand the reason for this particular snippet's problem)

like image 755
Tatha Avatar asked Jul 23 '26 06:07

Tatha


1 Answers

There is no logic applied that could affect the value of the day variable -- the complier can plainly see that it will always be daysoftheweek.FRI. The error is telling you that it will never equal daysoftheweek.MON so the if statement has no purpose.

like image 151
Will P. Avatar answered Jul 24 '26 21:07

Will P.



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!