JavaScript 静态类型检查的现状如何?

发布于 2024-11-19 18:08:48 字数 193 浏览 5 评论 0原文

我知道 Google Closure 编译器会进行类型检查,但是有没有其他替代方案,最好是不与库和优化器紧密耦合的替代方案?

如果没有,有没有办法让 Google Closure Compiler 进行静态分析?

(这里的静态分析是指定义参数的类型等,如果我输入错误或传递了错误的类型,我可以运行一些东西来给我警告。)

I know that the Google Closure Compiler does type checking—but are there any alternatives, preferably that aren't so tightly coupled with a library and optimizer?

If not, is there any way to have the Google Closure Compiler only do static analysis?

(By static analysis here, I mean things like defining types for arguments and so on that I can run through something to give me warnings if I make a typo or pass the wrong type.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空心空情空意 2024-11-26 18:08:49

Doctor JS,这是一个 Mozilla 项目,主要(至少据我所知)对 JS 进行类型检查。

There's Doctor JS, which is a Mozilla project that primarily (as I understand it, at least) does type-checking for JS.

别低头,皇冠会掉 2024-11-26 18:08:49

Microsoft 的 AJAX Minifier 对于您需要对 JS 文件进行的准备工作稍微宽松一些,以便从中获得有用的结果。您可以使用默认值运行它并得到一个高度缩小的文件,该文件仍然可以与外部代码一起使用:
http://ajaxmin.codeplex.com/

但是,Closure Compiler 和 Ajax Minifier 只能做非常有限的静态工作由于 Javascript 的设计方式,分析超出了基本的 linting。访问未声明的属性可能只是检查未定义,分配未声明的变量仅意味着在全局范围内声明它,将对象分配给包含数字的变量是合法的,等等。在 JS 中,有很多是合法的,而您的典型语言(Java、C#)考虑越界,因此如果不声明特定编译器的类型、边界和期望,不幸的是,您可以防止的错误会受到限制。

我对可以在两大巨头(微软和谷歌)之间转换的东西更感兴趣。它对于 IDE 支持、通过高级优化测试代码大小等很有用。

Microsoft's AJAX Minifier is a little more relaxed about the amount of prep you need to do to a JS file to get useful results out of it. You can run it with defaults and get out a highly minified file that still works with outside code:
http://ajaxmin.codeplex.com/

But, both Closure Compiler and Ajax Minifier can only do very limited static analysis beyond basic linting, because of how Javascript is designed. Accessing an undeclared property may just be checking for undefined, assigning an undeclared variable just means declaring it in the global scope, assigning an object to a variable that contained a number is legal, etc. There's a lot that's legal in JS that your typical language (Java, C#) considers out of bounds, so without declaring types, boundaries and expectations for a specific compiler you're unfortunately limited in the errors you can prevent.

I'd be a bit more interested in something that can transform between the big 2 (MS and Google). It would be useful for IDE support, testing code size with advanced optimizations, etc.

如日中天 2024-11-26 18:08:49

我对 intellij idea / webstorms 编辑器非常满意,它解析 jsdoc 并进行自己的静态分析以标记潜在或实际的类型安全错误。事实证明,它非常有用,尽管需要做一些工作才能让继承与一些通用框架一起工作。由于 javascript 原型继承可能有大量的方法,因此编译器比其他语言需要更多的帮助。

它是一个商业工具,但我可以将它用于 java、php、javascript、python 和 ruby​​ 项目,所有这些都带有一些相当不错的静态分析和重构助手。我曾经使用 emacs 做了很多工作,并为 jshint 和闭包编译器运行 node.js 进程,但这要容易得多。

I have been quite happy with the intellij idea / webstorms editor, which parses jsdoc and does its own static analysis to flag potential or actual type safety errors. It has proven quite useful, although a bit of work was needed to get inheritance to work with some common frameworks. Due to the tons of approaches possible with javascript prototypal inheritance, the compiler needs a bit more help than for other languages.

It's a commercial tool, but I'm able to use it for java, php, javascript, python and ruby projects, all with some pretty decent static analysis and refactoring helpers. I used to do a lot with emacs and running node.js processes for jshint and closure compiler, but this is a lot less brittle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文