codemix / babel-plugin-typecheck

Static and runtime type checking for JavaScript in the form of a Babel plugin.
MIT License
886 stars 44 forks source link

Objects with unknown props: Immutability check #41

Closed alex35mil closed 9 years ago

alex35mil commented 9 years ago

One of 2 main things I try to solve is to check:

  1. Is type of passed id String or Number.
  2. Is type of passed object is Plain JS Object or Immutable.js instance.

First one works great, but second one is tricky in case when I check Object with unknown set of props:

// Check Immutable instance
// Should work like this (except issue#40)
function dummiesReducer($immutableMap: $Map) {}

// Check Object with known props is also trivial
function dummiesReducer(obj: { knownProp: string }) {}

// But I'm not sure what's the best way to check
// that passed object with unknown props is Plain JS Object
// and not an Immutable.js instance
function dummiesReducer(obj: Object) {} // <--- Won't work as Immutable.js instance is also Object

// For now I ended up with this hack
function dummiesReducer(obj: { toJS: void }) {}

Is there any better way to perform such checks?

phpnode commented 9 years ago

I don't think that this is possible with flow's semantics. There's no way to say "match this type and none of its sub-types", only "match this type and all of its sub-types". I think your current solution is probably the only way around it.