Comparing enumeration values ​​in a stream

I use flow to comment types in my code.

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

Is there a way to teach me to flowreport a warning or error for comparison with non-conforming types ( stringin my case)?

here is a test example

edit: so it seems like this cannot be done with enums. but, since this is actually a mistake that I encountered, I would like to express this so that the flow will help me identify such a situation.

Any ideas for this?

+6
source share
2 answers

You can use the format (value: Type). In your case, it will be:

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

.

.

+3

, :

type Bar = 'One' | 'Two';

function eq(a: Bar, b: Bar): boolean {
  return a === b;
}
function foo(b: Bar) : boolean {
  // compare b to 'Three'
  return eq(b, 'Three');
}

return eq(b, 'Three');
                  ^ string. This type is incompatible with the expected param type of 
function eq(a: Bar, b: Bar): boolean {
                       ^ string enum

0

All Articles