There's a fair bit of work in this area that leverages Flow type syntax, e.g:
- https://github.com/gcanti/tcomb
- https://github.com/gcanti/io-ts (for typescript)
- https://github.com/codemix/flow-runtime (i wrote this)
And another alternative is to use Design by Contract, like Eiffel: