0

I'm having issues typing my code with flow. What I want to achieve is a recursive function that deep freezes and object. I have the following implementation:

const isObject: (any => boolean) = object => (
  object !== null && typeof object === 'object'
)

type TObject = Object | any[]
export const deepFreeze: (TObject => TObject) = object => {
  // Actually this doesn't need to be fixed, because an array is an object and
  // it's properties can thus be enumerated.
  // $FlowFixMe
  const ownPropNames = Object.getOwnPropertyNames(object)

  each(ownPropNames, name => {
    const prop = object[name]
    if (isObject(prop)) deepFreeze(prop)
  })

  return Object.freeze(object)
}

This code works for objects and arrays (which technically are objects as well) alike. But flow won't accept it, not even with the $FlowFixMe comment. Flow utters the following:

Screenshot of flow error

I guess this is because Object.getOwnPropertyNames is typed to only accept objects, because if I call it with an empty object instead of my variable, the errors are gone. They are also gone if I replace TObject by Object, but then of course I cannot call the function with an array. So

  1. why is this error message so damn confusing? It doesn't have to do anything with the problem (if I interpreted it correctly)
  2. why does it not accept an array, which it should, as it's an object that has enumerable property names (namely '0', '1', ..., 'length')

Suggestions would be greatly appreciated.

gerrit
  • 391
  • 3
  • 11

0 Answers0