PudgePacket PudgePacket - 1 year ago 65
Javascript Question

Flow doesn't complain about incorrect type

In this piece of code, flow is not complaining about the value

being set on the state. It seems to be ignoring the
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
such as
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) {
return {
[action.namespace]: {
project: action.project,
collection: action.collection,
dog: 1,
return state

export default namespaceReducer

Answer Source

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,
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download