Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate type checking in JavaScript at run time?

With TypeScript or Facebook's Flow(type) I can statically type variables like

function add (x: integer, y: integer) { ... }

Both TypeScript and Flow catch any illegal invocation such as add('1',0) at compile time.

However, when the library is compiled and exported, the types are gone. That means, the library consumer using that function gets no errors, that may potentially lead to hard-to-debug problems.

Is there any way to auto-generate additional code that would throw exactly the same errors at run time?

I can surely manually place guards every time a typecheck is expected but that feels boring and repetitive.

like image 230
Dmitri Zaitsev Avatar asked May 11 '26 13:05

Dmitri Zaitsev


1 Answers

https://github.com/codemix/babel-plugin-typecheck does what you want.

There is also https://gcanti.github.io/flowcheck/ but it looks a bit abandoned.

I haven't personally used any of those libraries. It seems that they might cover less than 100% of Flow syntax.

like image 177
Nikita Avatar answered May 13 '26 02:05

Nikita



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!