Type checker for JavaScript?

断了今生、忘了曾经 提交于 2019-12-08 17:40:30

问题


Does anyone know if there's a good tool for analyzing JavaScript code and detecting type errors? I know that JavaScript itself is weakly and dynamically typed, but it would be really nice if I could have a program that would verify that all my field accesses are sensible and that I don't try treating a number like a string, for example. I'm aware that there are valid use cases in JavaScript where adding or removing fields or converting between different types is valid and expected, but some errors are so blatant it seems like they could be caught before the script started running.

I've heard of JSLint, but my understanding is that it's mostly a style tool rather than a semantic analyzer. If I'm wrong about this, then just telling me so would be a valid answer to this question.


回答1:


Google closure compiler does some type checking if you follow their recommendations for annotating your functions with jsDoc comments. See http://code.google.com/closure/compiler/docs/js-for-compiler.html




回答2:


One another option - https://github.com/yarax/typelint

It's an EsLint plugin, that does type check based on JSDoc annotations.

In addition it supports it's own extension to use Redux state or Swagger schemas (basically any JSON schema) for type check. So you don't need to describe complex composite types by yourself.




回答3:


It's not possible to do static-analysis with confidence using any automated tool owing to the dynamic nature of JavaScript. However, one tool that does a great job is Jetbrain's WebStorm and their other IDEs that have JavaScript support.




回答4:


There is new type checker from Facebook - flow.

And it has a plugin that would convert your JSDoc annotations to flow annotations.



来源:https://stackoverflow.com/questions/4929880/type-checker-for-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!