Hacker News new | ask | show | jobs
by aphexairlines 3080 days ago
> I validate an object manually and I still have to do an "any" cast in Flow because Flow does not recognize that my `if` statement secures the type.

Try this instead:

    import { reify } from 'flow-runtime'
    import type { Type } from 'flow-runtime'
    type YourData = { ... }
    const unknownObject: mixed = ...
    const validatedObject: YourTarget = (reify: Type<YourData>).assert(unknownObject)

See https://github.com/codemix/flow-runtime/issues/150 and https://codemix.github.io/flow-runtime/#/docs/type-refinemen...

The `reify` marker from flow-runtime and the accompanying Babel plugin will produce a runtime type object that can validate your data at runtime according to your Flow types.