Flow: Refinement of "Generic" Parameters

Created on 19 Jan 2017  路  4Comments  路  Source: facebook/flow

Two examples (same results for Flow 0.37.0 and 0.38.0):

function justReturn<T>(x: T): T {
  if (typeof x == "number") {
    return x + 1; // <---
  }
  return x;
}
function justReturn<T>(x: T): T {
  if (typeof x == "number") {
    return 1; // <---
  }
  return x;
}

// 5:     return 1; // <---
//               ^ number. This type is incompatible with the expected return type of
// 3: function justReturn<T>(x: T): T {
//    ^ some incompatible instantiation of `T`

The first compiles. The second does not. Is either behaviour intentional?

(I had a whinge about the problems the first one introduces, but I figured it was a deliberate choice. The second one seems like a bug. But I don't know what was _really_ intended in either case.)

bug

Most helpful comment

First one is clearly a bug:

function justReturn<T>(x: T): T {
  if (typeof x == "number") {
    return x + 1;
  }
  return x;
}

let x: 42 = 42;
let y: 42 = justReturn(x);

All 4 comments

First one is clearly a bug:

function justReturn<T>(x: T): T {
  if (typeof x == "number") {
    return x + 1;
  }
  return x;
}

let x: 42 = 42;
let y: 42 = justReturn(x);

This is related to "magic" + semantics, works correctly with other operations

Minimal test case:

function test<T>(x: T) {
  if (typeof x == "number") {
    ((x + 1): T)
  }

Probably has nothing to do with refinements:

function test<T:number>(x: T) {
    ((x + 1): T)
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ctrlplusb picture ctrlplusb  路  3Comments

bennoleslie picture bennoleslie  路  3Comments

philikon picture philikon  路  3Comments

davidpelaez picture davidpelaez  路  3Comments

Beingbook picture Beingbook  路  3Comments