Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Add flow types to dispatcher #243

Merged
merged 1 commit into from
Jul 19, 2015
Merged

Add flow types to dispatcher #243

merged 1 commit into from
Jul 19, 2015

Conversation

kyldvs
Copy link
Contributor

@kyldvs kyldvs commented Jul 18, 2015

Making dispatcher polymorphic allows strong type checking with flow

type Action =
  {
    type: 'foo',
    foo: number,
  } |
  {
    type: 'bar',
    bar: string,
  };

var FooDispatcher: Dispatcher<Action> = new Dispatcher();

function acceptsNumber(x: number): void {}

FooDispatcher.register(function(action: Action) {
  switch (action.type) {
    case 'foo':
      acceptsNumber(action.foo);
      acceptsNumber(action.bar); // flow error, property not found in object
      break;

    case 'bar':
      acceptsNumber(action.bar); // flow error, string incompatible with number
      break;
  }
});

type NotAction = string;
FooDispatcher.register(function(action: NotAction) {}); // flow error

// flow error
FooDispatcher.dispatch({
  type: 'foo-typo',
  foo: 100,
});

@camspiers
Copy link

👍 👍 awesome

kyldvs added a commit that referenced this pull request Jul 19, 2015
Add flow types to dispatcher
@kyldvs kyldvs merged commit ac92a45 into facebookarchive:master Jul 19, 2015
@gaearon
Copy link

gaearon commented Jul 19, 2015

This is great! We want that in Redux, too. How does it work with NPM?

Surely if you just compile to JS, the annotations are stripped. How do you intend to keep them in NPM for Flow consumers? Is there an established pattern for this?

Thanks.

@ghost
Copy link

ghost commented Jul 19, 2015

@gaearon http://flowtype.org/blog/2015/02/20/Flow-Comments.html
put flow annotations as comments and change your js output workflow so that it keeps comments.

@kyldvs
Copy link
Contributor Author

kyldvs commented Jul 19, 2015

I haven't looked closely at how to make it work with NPM yet. It might be possible to just include a lib file with only the flow types of Dispatcher that can be included under the [libs] section of the .flowconfig.

@kyldvs kyldvs deleted the flow branch July 19, 2015 20:39
@kyldvs
Copy link
Contributor Author

kyldvs commented Jul 19, 2015

It looks like babel support emitting flow comments, babel/babel#1698

@gaearon
Copy link

gaearon commented Jul 19, 2015

Yes, indeed! Thank you.

@albandiguer
Copy link

neat!

@leoasis
Copy link

leoasis commented Jul 26, 2015

Does Flow actually handle those type errors? According to the docs in the Flow site it seems to not do that, and I'm testing a similar case with Redux and failing to make it type check.

Here's the doc found in the Flow site: http://flowtype.org/docs/react-example.html#narrowing-union-types

"Ideally, we would want to narrow down the type of action inside the individual cases. However, this is currently not possible with Flow. Not all hope is lost though. Flow supports any as type."

@kyldvs
Copy link
Contributor Author

kyldvs commented Jul 27, 2015

Hmm, yeah I ran this exact code in my example and added the comments where there were actual errors. This is on flow v0.13.1

@leoasis
Copy link

leoasis commented Jul 27, 2015

Yeah thanks @kyldvs, finally figured out how to do this. Seems like those docs are outdated then.

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

Successfully merging this pull request may close these issues.

6 participants