Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flow doesn't complain about incorrect type

In this piece of code, flow is not complaining about the value dog being set on the state. It seems to be ignoring the NamespaceData definition. I've set up the types so it should complain. I'm running on nuclide and flow is working properly for everything else.

All of the properties of action such as namespace, project, collection are strings.

// @flow

import { NAMESPACE_SET } from '../actions/NamespaceActions'

type NamespaceData = {
  project: string,
  collection: string,
}

type NamespaceState = {
  [namespace: string]: NamespaceData,
}
const initialState: NamespaceState = {}

function namespaceReducer(state: NamespaceState = initialState, action: Object): NamespaceState {
  switch (action) {
    case NAMESPACE_SET: {
      return {
        ...state,
        [action.namespace]: {
          project: action.project,
          collection: action.collection,
          dog: 1,
        }
      }
    }
  }
  return state
}

export default namespaceReducer
like image 491
Trent Avatar asked Nov 22 '16 00:11

Trent


1 Answers

Flow is not strict about unknown properties in objects by default, e.g.

// @flow

type Thing = {
  known: string;
};

var obj: Thing = {
  known: 'hi',
  unknown: 4,
};

typechecks fine even though unknown is not in the type.

Flow 0.32 includes

  • New syntax for exact object types: use {| and |} instead of { and }. Where {x: string} contains at least the property x, {| x: string |} contains ONLY the property x.

In your example you'd want exact object syntax with:

type NamespaceData = {|
  project: string,
  collection: string,
|};
like image 192
loganfsmyth Avatar answered Oct 22 '22 18:10

loganfsmyth