Flow: Major: Function type with Date argument accepts a function with number argument

Created on 19 Dec 2017  路  9Comments  路  Source: facebook/flow

Flow verisons

Seems to happen on any version I've tried so far:

  • 0.37
  • 0.40
  • 0.42
  • 0.50
  • 0.57.1
  • 0.61

Code (Try flow link)

type DateCallback = (value: Date) => Promise<any>

function foo(value: number): Promise<any> {
  return Promise.resolve()
}

(foo: DateCallback);

Expected

7: (foo: DateCallback);
     ^ function. This type is incompatible with
7: (foo: DateCallback);
          ^ function type
This parameter is incompatible:
1: type DateCallback = (value: Date) => Promise<any>
                               ^ Date. This type is incompatible with
3: function foo(value: number): Promise<any> {
                       ^ number

Actual

No errors!

Most helpful comment

@jedwards1211 looks like you are correct. Removing instances of Date from the numeric test at https://github.com/facebook/flow/blob/master/src/typing/flow_js.ml#L7312-L7313 fixes this particular issue, but makes it impossible to perform numeric operations on Dates

All 9 comments

Reduced case:

declare var x: Date;
(x: number); // no error

whoa!

It must have to do with making Flow support comparisons between numbers and Dates

This a major was a major "WAT" moment for me? Is this intentional? One would expect this to throw an error:

const time: number = new Date();

@jedwards1211 looks like you are correct. Removing instances of Date from the numeric test at https://github.com/facebook/flow/blob/master/src/typing/flow_js.ml#L7312-L7313 fixes this particular issue, but makes it impossible to perform numeric operations on Dates

One solution might be to remove the numeric attribute of Date and force users to do Number(myDate) > Number(theirDate).

Agreed. Date and number share some common arithmetic operations but they're definitely not the same type.

I think it's possible to fix this issue without breaking support for number + Date and so forth. Explicit casting doesn't have to be necessary.

Funny you should mention number + Date! Turns out + stringifies Date, and all the other numeric operators numberifies Date.
image
Meanwhile flow happily accepts const a: number = new Date() + 12;

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jamesisaac picture jamesisaac  路  44Comments

Macil picture Macil  路  186Comments

danvk picture danvk  路  73Comments

StoneCypher picture StoneCypher  路  253Comments

cletusw picture cletusw  路  52Comments