1
var bug = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10

I am encountering this type of pattern in React and was confused by what it does. When I tried with numbers I get 15. How does that expressions sum to 15?

I realize now this is a Facebook Flow thing. I don't believe is Bitwise OR as marked.

Mörre
  • 5,560
  • 5
  • 35
  • 62
chackerian
  • 759
  • 8
  • 22
  • 5
    Do you know what the `|` operator does? – XCS Aug 27 '17 at 14:40
  • 2
    Why do you think that expression is a sum? – Barmar Aug 27 '17 at 14:41
  • Oh I was confusing it with the `||` operator, sorry – chackerian Aug 27 '17 at 14:44
  • It's definitely a weird pattern though, given that it could have been replaced by a constant. Where exactly did you find this? – Bergi Aug 27 '17 at 14:51
  • A lot of places, here: `export type TypeOfWork = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;` in ReactTypeofWork.js and some other places. – chackerian Aug 27 '17 at 15:18
  • I think I actually understood what `|` did but I just didn't believe that type of operator was being used in things like the above. – chackerian Aug 27 '17 at 15:24
  • 1
    @chackerian Well that is whole different thing. It's a `type`, not a `var` as in your question, which means that it's TypeScript code where the operator means a completely different thing than bitwise OR. (While I don't know TypeScript, it sure looks like a separator between enum values). – Bergi Aug 27 '17 at 16:13

0 Answers0