Skip to content

Flow 0.46 Arity Flag #1223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
deecewan opened this issue May 11, 2017 · 16 comments
Closed

Flow 0.46 Arity Flag #1223

deecewan opened this issue May 11, 2017 · 16 comments

Comments

@deecewan
Copy link

deecewan commented May 11, 2017

Flow has shipped a strict_call_arity flag, which will become the only option as of 0.47.

With this flag enabled, there are issues using the Map constructor, at a minimum (I haven't tried the other data types).

MCVE:

  • using flow-bin v0.46 and the strict_call_arity flag set
  • using Immutable 3.8.1
/* @flow */

import { Map } from 'immutable';

const myMap = new Map({ testKey: 'test value' });

resulting in the following error:

testfile.js:26
 26: const myMap = new Map({ testKey: 'test value' });
                           ^^^^^^^^^^^^^^^^^^^^^^^^^ unused function argument
       v------------------------------------------
   34: declare class _Iterable<K, V, KI, II, SI> {
   35:   static Keyed:   KI;
   36:   static Indexed: II;
  ...:
  187: }
       ^ default constructor expects no arguments. See: node_modules/immutable/dist/immutable.js.flow:34
@philipp-spiess
Copy link
Contributor

I see the same error. Also happens on Immutable 4.0.0-beta.2

@philipp-spiess
Copy link
Contributor

philipp-spiess commented May 29, 2017

Flow 0.47.0 shipped with experimental.strict_call_arity on per default and no option to disable it.

@wschurman
Copy link

Hit this as well. The solution is to not use the new keyword when constructing immutable objects (which surprised me as well). The documentation doesn't indicate that constructing these objects with new is supported.

If new is intended to be supported, then the correct solution is to duplicate definitions for the nameless static methods with constructor as their names.

@deecewan
Copy link
Author

thanks for that. I might open a PR with that as the fix (unless you want to), solely because my linter doesn't like upper-first methods without the new keyword.

Seems weird that immutable doesn't allow this, or use it more in the docs. Is there any reason not to use it?

@wschurman
Copy link

Yeah, not too sure about the why. The only place that I could see it being confusing is for Records, which have new explicitly documented and a typedef.

I'd say a PR is probably a good way to get a discussion going here or at least get an answer. I had one started but abandoned it once I realized none of the tests used new.

@deecewan
Copy link
Author

deecewan commented May 30, 2017

ahhh okay. fair enough. It just seems weird because then, if I'm not mistaken,

import { List } from 'immutable';

const x = new List([]);
const y = List([]);

console.log(x instanceof List); // true
console.log(y instanceof List); // false

unless the function calls the constructor if it's not explicitly called.

EDIT:

looks like they're calling the constructor if it's not called.

image

This is the output I expected:

image

@rahulthewall
Copy link

So @deecewan, is the solution simply to drop the new keyword in order to stop flow from complaining?

@deecewan
Copy link
Author

That is correct, although I'm not super happy with that as a solution. It seems the solution would be to fix the type definitions. It feels wrong instantiating a new object of a certain type without the new keyword.

@rahulthewall
Copy link

I agree, I am not fixing it in my project and would wait for an update to the type definitions.

@MSch
Copy link

MSch commented Jun 27, 2017

Has anyone found a way to change the typedefs so flow doesn't error any more?

@villesau
Copy link

Is there any progress with this?

@deecewan
Copy link
Author

deecewan commented Aug 11, 2017

@MSch to do this, you'd need to add a constructor that matched the static method of each class.

For Map, for example, duplicating lines 686 & 687, and replacing static with constructor would work, I believe.

It should look like

static <K, V>(obj?: {[key: K]: V}): Map<K, V>;
static <K, V>(collection: Iterable<[K, V]>): Map<K, V>;
constructor <K, V>(obj?: {[key: K]: V}): Map<K, V>;
constructor <K, V>(collection: Iterable<[K, V]>): Map<K, V>;

That being said, I haven't tested this, and there would need to be a bunch of new tests written to ensure the libdefs were correct.

@ifeanyi
Copy link

ifeanyi commented Aug 12, 2017

You can disable strict function call arity by adding the following to your .flowconfig

experimental.strict_call_arity=false

See https://flow.org/blog/2017/05/07/Strict-Function-Call-Arity/

@arv
Copy link

arv commented Aug 12, 2017

Flow v0.47.0 will ship with strict function call arity turned on and the experimental.strict_call_arity flag will be removed.

@ifeanyi
Copy link

ifeanyi commented Aug 12, 2017

@leebyron
Copy link
Collaborator

Thanks for the report - in general using new is discouraged with Immutable factory functions. They are similar to the String() or Number() factories.

Looks like we have these flow errors fixed in what will be the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants