Trent
Trent

Reputation: 601

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

Upvotes: 6

Views: 141

Answers (1)

loganfsmyth
loganfsmyth

Reputation: 161477

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,
|};

Upvotes: 7

Related Questions