-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Run-time Type Checks #7607
Comments
I would say a compiler flag is too broad, and a decorator like |
...also, I think you could write said decorator yourself, with type guard functions, except that you will likwly have to specify types explicitly, e.g |
Babel provides something along the lines of this request: https://github.com/codemix/babel-plugin-typecheck (linked from the same request for Flow: facebook/flow#897 ) |
Duplicate of #1573 |
Hi there!
I am writing a lot of code in an environment where JavaScript and TypeScript freely interact with each other. One thing that we continue to run into is where we would write a function in TypeScript, say:
This function is callable from JavaScript, where we're allowed to do things like:
This has lead us to introduce type checks for all exported functions in TypeScript, although they really feel verbose in a typed language.
My idea is to provide an optional flag, say
--insertRuntimeTypeChecks
, that makes the compiler insert these kinds of checks for function arguments.The feature will probably be a bit more complicated when thinking about structural types, etc., especially in interplay with classes (it is my understanding that classes are type-checked structurally, not nominally, is that correct?).
But even having it only for primitive types (checking for
object
,string
,number
, etc.) could provide a lot of benefits for those of us who write code that interacts with JavaScript, especially should #7140 land, to spare us from manually writingif (x === null) ...
.I'd be interested in giving implementing this a shot, if there's interest from the maintainers.
The text was updated successfully, but these errors were encountered: