Flow: assert() won't work

Created on 20 May 2015  路  9Comments  路  Source: facebook/flow

I have noticed that assert() function (from node.js' stock assert module) does not seem to work:

/* @flow */
var assert = require('assert');
assert('abc', true);

If you use ok property, flow can detect the type error correctly:

/* @flow */
var assert = require('assert');
assert.ok('abc', true);

In fact, even giving a correct code like below, flow complains with message, "Could not resolve name":

/* @flow */
var assert = require('assert');
assert(true); // <== this is correct

Most helpful comment

Question: shouldn't assert/assert.ok strip maybe-types to their inner types?
assert() can never return undefined, right?

All 9 comments

It's true that lib/node.js does not define the assert module correctly. Currently flow does not recognize that the module's export is a "callable object," a.k.a. a function with properties. Instead, the export is declared to be an object with fail, ok, equal, etc. properties.

I am not certain whether it is currently possible to declare this kind of export with the existing syntax, so for now the only workaround is to use the ok form. Sorry about the inconvenience.

@gabelevi showed me how to declare callable object signatures, so this is fixed in #445.

Could you please show me how it (what @gabelevi showed you) can be declared? I am having hard time writing interfaces for other (custom) modules too, where the exports being a function object, or a class - same thing. Thanks!

@enobufs Does the code change in #445 serve as an adequate example?

Oops! Not #445... The correct PR is #455. Sorry!

Awesome!
I came across #73 last night and I am wondering if that could also be resolved with the similar way?
Thank you.

Thanks for the pointer to that issue! Looks like the special exports declaration will solve that one as well. Cheers!

@enobufs The fix for this has landed in master, and should be included in the next release. Thanks for the report!

Question: shouldn't assert/assert.ok strip maybe-types to their inner types?
assert() can never return undefined, right?

Was this page helpful?
0 / 5 - 0 ratings