DEV Community

Discussion on: Validate javascript data type in runtime? Yes, Hello Type.

Collapse
stereobooster profile image
stereobooster

Checkout my post about IO validation, it shows how to make a bridge between static types (Flow or TypeScript) and runtime type checking.

Collapse
frustigor profile image
frustigor Author

That's great idea. However, it seems to be one thing no matter in Flow or in Typescript. For example, you should write a function assertPerson in which you must check its input and output with Flow syntax:

const assertPerson = (val: mixed): Person => is(val, PersonInterface, "Person")

It seems to make it metaphysics if we follow Flow.

And your example for io-ts, it use t.type to generate a PersonInterface and use PersonInterface.decode to check data type, which is like what HelloType did.

So I think, if I want to, I can create a bridge too😊

Collapse
stereobooster profile image
stereobooster

For example, you should write a function assertPerson in which you must check its input and output with Flow syntax

Not sure what your concern here, but if you worry that you will need to use flow syntax and it will be incompatible with good old JS, this is not the case. See source code of sarcastic it is absolutely valid JS, but at the same time, it is type-checked by Flow.

So I think, if I want to, I can create a bridge too

For sure. I think it will be a cool feature. Even cooler if you can get support for both TypeScript and Flow. πŸ‘

Thread Thread
frustigor profile image
frustigor Author

for the first point. I do not think flow will help you to check data type during run time, in fact, the checking is done by your own code logic, not by flow static system.

And in my opinion, Flow or Typescript make a way to access static data type, but weaken js flexibility, and make project code library bigger and bigger. It is for a big project with different level of developers, not for small projects or which developed by experienced developers.

Thread Thread
stereobooster profile image
stereobooster

Checkout my post about IO validation, it shows how to make a bridge between static types (Flow or TypeScript) and runtime type checking.

If you wonder why one need types, read my post on how types help to prevent bugs here