Flow: The (+) operator incorrectly accepts generic types

Created on 23 Mar 2016  路  1Comment  路  Source: facebook/flow

In the following code:

function identity<A>(x: A): A {
  return x + x;
}

// $ flow
// No errors!

I expected Flow to give me a type error in x + x, because the abstract type A is incompatible with the types expected by the + operator. However, Flow incorrectly says that there are no type errors in this code.

Other operators work as expected:

function identity<A>(x: A): A {
  return x - x;
}

// $ flow
// id.js:4
//   4:   return x - x;
//               ^ A. This type is incompatible with
//   4:   return x - x;
//               ^^^^^ number
// 
// id.js:4
//   4:   return x - x;
//               ^^^^^ number. This type is incompatible with
//   3: function identity<A>(x: A): A {
//      ^ some incompatible instantiation of `A`
// 
// id.js:4
//   4:   return x - x;
//                   ^ A. This type is incompatible with
//   4:   return x - x;
//               ^^^^^ number
// 
// 
// Found 3 errors
bug

Most helpful comment

@mroch fixed this in 0.33. Error output is now:

4:   return x + x;
            ^ A. Cannot be added to
4:   return x + x;
                ^ A

>All comments

@mroch fixed this in 0.33. Error output is now:

4:   return x + x;
            ^ A. Cannot be added to
4:   return x + x;
                ^ A
Was this page helpful?
0 / 5 - 0 ratings