Flow: Addition between null/undefined and a number doesn't throw error .

Created on 13 Sep 2016  路  4Comments  路  Source: facebook/flow

I searched the docs and issues for a while, but didn't see anything.

// @flow

console.log({} + null) // error

console.log(1 + null) // not error
console.log(1 + undefined) // not error
index.js:3
  3: console.log({} + null) // error
                 ^^ object literal. This type cannot be added to
  3: console.log({} + null) // error
                 ^^^^^^^^^ string

index.js:3
  3: console.log({} + null) // error
                      ^^^^ null. This type cannot be added to
  3: console.log({} + null) // error
                 ^^^^^^^^^ string

Is there any reason for this or anything I can do to make it typecheck short of making my own addition function const add = (x: number, y: number): number => x + y ?

linter

Most helpful comment

You can "sneak" NaNs into the type system this way:

The following will throw a null/undefined. The operand of an arithmetic operation must be a number. error as expected:

/* @flow */
function foo(x: ?number): number {
  return 1 - x;
}

try here

Yet changing - to + will make it check:

/* @flow */
function foo(x: ?number): number {
  return 1 + x;
}

and everything is "fine" try here

except it will cause an NaN for null or undefined as input.

Is there a way to type check addition with maybe types in flow-typed safely? Or is this a bug like this? (in which case I will happily file a bug report)

[email protected]

(Asked at stackoverflow)

All 4 comments

here's the commit that added the code that allows this, but I acknowledged at the time that we might want to tighten it further so the fact that it's explicitly allowed in the code shouldn't imply that it was explicitly intended to work this way :)

I think there's some debate about whether this should be an error, since null and undefined have well-defined coercion behavior, or whether it should be a "sketchy null" that a hypothetical type-aware linter would complain about.

Personally I'd like to make it error, and see whether it creates a usability problem.

Ah ok. What are the cases when you wouldn't want it to error out?

I think it could be helpful to make it an error because you would never intentionally want something like null + number or undefined + number.

You can "sneak" NaNs into the type system this way:

The following will throw a null/undefined. The operand of an arithmetic operation must be a number. error as expected:

/* @flow */
function foo(x: ?number): number {
  return 1 - x;
}

try here

Yet changing - to + will make it check:

/* @flow */
function foo(x: ?number): number {
  return 1 + x;
}

and everything is "fine" try here

except it will cause an NaN for null or undefined as input.

Is there a way to type check addition with maybe types in flow-typed safely? Or is this a bug like this? (in which case I will happily file a bug report)

[email protected]

(Asked at stackoverflow)

Was this page helpful?
0 / 5 - 0 ratings