Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing enum values in Flow

I'm using flow to annotate types in my code.

type Bar = 'One' | 'Two';
function foo(b: Bar) : boolean {
  return b === 'Three';
}

Is there any way to teach flow to report a warning or an error for comparisons with non matching types (string in my case)?

here is the example for test

edit: so it seems not to be possible to do it with enums. but, since this is actually an error I encountered, I'd like to express this so that flow will help me flag such a situation.

Any ideas for that?

like image 288
oliver Avatar asked Aug 07 '17 12:08

oliver


1 Answers

You can use the format (value: Type) . In your case that would be:

type Bar = 'One' | 'Two';
function foo(b: Bar) : boolean {
  return b === ("Three": Bar);
}

Which will show the error.

Updated example.

like image 191
Meligy Avatar answered Oct 06 '22 00:10

Meligy