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.)
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)
}
Most helpful comment
First one is clearly a bug: