2
votes

I've been slowly adding Flow type annotations to a small Redux application. However, I'm stuck on adding an annotation for the state in the reducer.

The application in question uses Immutable.js, and the state parameter is an instance of Immutable.Map. Try as I might, I can't figure out how to annotate that.

Here is the reducer in question:

// @flow
import {List, Map, fromJS} from 'immutable';

type Action = {
  type: string,
  content: mixed
};

export default function(state = Map(), action: Action) {
  switch (action.type) {
  case 'ADD_LINK': {
    let links = state.get('links').push(fromJS(action.content));
    return state.set('links', links);
  }
  case 'UPDATE_FILTER':
    return state.set('filter', fromJS(action.content));
  default:
    return state;
  }
}

Any idea how I can add a type annotation for the state as an Immutable.js map? I've tried the following without success, and now I'm stumped:

import type { Map } from 'immutable';

export default function (state: Map = Map(), action: Action) {
1

1 Answers

2
votes

You can describe the state as the following:

export default function(state: Map<string, mixed> = Map(), action: Action)
{
    // ...
}

Also, there is a better way to describe the Action:

export type Action = {
    type: 'ADD_LINK' | 'UPDATE_FILTER'
  , content: mixed
};

Now invalid action strings would be detected...

Please note, that to call your function you have to explicitly convert a literal object to Map:

let res = blah(Map({foo: 'bar'}), {type: 'ADD_LINK', content: 'blah-blah'});