Flow: Arbitrary properties of function object have type any

Created on 22 Nov 2014  路  7Comments  路  Source: facebook/flow

Search keywords: function, statics

Flow finds no error in the following code:

/* @flow */
function noop(){}
console.log(noop.foo/noop.bar);

I think it is a bug because both noop.foo and noop.bar are undefined.

soundness enhancement

Most helpful comment

@avikchaudhuri I see you mentioned 5 years ago that this will be fixed. Just pinging to see if there's any progress :D

All 7 comments

Known issue, will fix. (A function has an underlying object that stores its static properties; in general we keep objects created inside a module extensible, so we don't immediately error out on property accesses because they may be defined in other parts of the file the type checker hasn't yet visited. But we should be stricter in cases like the one you point out.)

Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed.

Would be curious to hear if there's an update on this.

Any news on this ?

@avikchaudhuri Do you think defaulting to ?mixed instead of any will be easy to do for this? That seems to solve the problem.

Just re-activating this thread, by pointing out that this weakness in Flow's type system does lead to real-world bugs, particularly around the use of promises.

For instance:

function getPromise(): Promise<boolean> {
  return Promise.resolve(true)
}

// type-checks, works at runtime
getPromise().then(() => true);

// type-checks, fails at runtime
getPromise.then(() => true);

https://flow.org/try/#0GYVwdgxgLglg9mABAcwKZQAoCc4FsYDOqAFAJQBci2ehqAPAEZxwA2qAhmAHyIDeAUIkRZ0ILEmr4iAOhEFWANxJQsIVKX4BffvzSYcUkqWlQAFqjDEyiALw8Va0gG5d6SbRPnL1u4gfqnIA

I understand that functions are ultimately objects in JavaScript, but so are arrays, and they have stricter type-checking in Flow. Why not functions too?

// type-checks, fails at runtime
({}).blah(() => 'hey')

// type-checks, fails at runtime
(() => {}).blah(() => 'hey')

// doesn't type-check, fails at runtime
[].blah(() => 'hey')

Any insights you can give into the decisions around this would be much appreciated!

@avikchaudhuri I see you mentioned 5 years ago that this will be fixed. Just pinging to see if there's any progress :D

Was this page helpful?
0 / 5 - 0 ratings