javascript ECMAScript 6 或 7 中是否支持静态类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22407765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there support for static typing in ECMAScript 6 or 7?
提问by Mojtaba
Is there any support for static typing in ECMAScript 6? How about ECMAScript 7?
ECMAScript 6 是否支持静态类型?ECMAScript 7 怎么样?
采纳答案by Daniel K. Mueller
No.
不。
But on the ECMA-Script Wikipage there is a paragraph about changes in ECMA-Script 7:
但是在 ECMA-Script Wikipage 上有一段关于 ECMA-Script 7 变化的段落:
The Seventh Edition is in a very early stage of development, but is intended to continue the themes of language reform, code isolation, control of effects and library/tool enabling from ES6. New features proposed include promises/concurrency, number and math enhancements, guards and trademarks (an alternative to static typing), operator overloading, value types (first-class number-like objects), new record structures (records, tuples and typed arrays), pattern matching, and traits.
第七版处于非常早期的开发阶段,但旨在延续 ES6 的语言改革、代码隔离、效果控制和库/工具启用等主题。提议的新特性包括承诺/并发、数字和数学增强、守卫和商标(静态类型的替代方案)、运算符重载、值类型(一流的类数字对象)、新的记录结构(记录、元组和类型化数组) 、模式匹配和特征。
Which may interest you.
你可能会感兴趣。
回答by Richard Connamacher
Though this isn't part of the ES6 spec, Closure Compilerenforces JSDoc argument type annotations in JavaScript code when using its Advanced compilation level. Type annotations are specified using comments so they're ignored in development, but when you build your app for a production release a type mismatch will result in a compiler warning or, optionally, a compiler error.
尽管这不是 ES6 规范的一部分,但Closure Compiler在使用其高级编译级别时会在 JavaScript 代码中强制执行 JSDoc 参数类型注释。类型注释是使用注释指定的,因此它们在开发中会被忽略,但是当您为生产版本构建应用程序时,类型不匹配将导致编译器警告或编译器错误(可选)。
An example of an enforced JSDoc type annotation:
强制 JSDoc 类型注释的示例:
/**
* @param {string} stringValue
* @return {number}
*/
function toInt(stringValue) {
return parseInt(stringValue, 10);
}
var val = toInt("10"); // Good
var val = toInt(false); // NaN in development, but throws an error (optional)
// or prints a warning (default) at build time
As an added bonus, JSDoc can build API documentation using this same syntax. So it's also handy if you document your code.
作为一个额外的好处,JSDoc 可以使用相同的语法构建 API 文档。所以如果你记录你的代码也很方便。
But a warning: for Closure Compiler to do its advanced optimization magic, every engineer on your project has to follow certain strict coding conventions. The compiler cannot enforce types unless it can reliably figure out what your code is doing, and that means giving up some of JavaScript's dynamic and wishy-washy syntax. If you don't follow them, errors can creep into your app and they can be very hard to diagnose after the fact. Most popular JavaScript frameworks and libraries do not follow them, though you can sometimes work around that using Compiler's externs feature. (jQuery is supported using externs, for example.)
但警告:为了让 Closure Compiler 发挥其高级优化魔法,项目中的每个工程师都必须遵循某些严格的编码约定。除非编译器能够可靠地弄清楚您的代码在做什么,否则编译器无法强制执行类型,这意味着放弃一些 JavaScript 的动态和一厢情愿的语法。如果您不遵循它们,错误可能会蔓延到您的应用程序中,并且事后很难诊断。大多数流行的 JavaScript 框架和库都没有遵循它们,尽管您有时可以使用 Compiler 的 externs 功能解决这个问题。(例如,使用 extern 支持 jQuery。)
So if you do use it, make sure you test your app thoroughly. I personally wouldn't even consider using this feature on a web app unless it has a Jenkins build bot and near-100% automated test coverage that can be run against your code afterit's been optimized. That's a lot of work and is not for everyone; it took me months to get one of my projects up to that level. But personally, I think it's well worth the effort.
因此,如果您确实使用它,请确保彻底测试您的应用程序。我个人甚至不会考虑在 Web 应用程序上使用此功能,除非它具有 Jenkins 构建机器人和近 100% 的自动化测试覆盖率,可以在优化后针对您的代码运行。这是一项繁重的工作,并不适合所有人;我花了几个月的时间让我的一个项目达到这个水平。但就个人而言,我认为这是值得的。
For more information, check out Advanced Compilation and Externsand Annotating JavaScript for the Closure Compiler.
有关更多信息,请查看Closure Compiler 的Advanced Compilation and Externs和Annotating JavaScript。
回答by Thomas Foster
No, there is no support for static typing in either ECMAScript 6 (ES2015).
不,ECMAScript 6 (ES2015) 都不支持静态类型。
As for ECMAScript 7 (ES2016), there is no proposal at any of stages 1, 2, 3, 4or stage 0for static typing.
至于 ECMAScript 7 (ES2016),在阶段 1、2、3、4或阶段 0中的任何一个阶段都没有关于静态类型的提议。
I've seen a few proposals/ideas for static typing appear on the es-discussmailing list, but none of these have actually been proposed for ES7 (ES2016).
我在es-discuss邮件列表中看到了一些关于静态类型的建议/想法,但实际上这些建议/想法都没有针对 ES7 (ES2016) 提出。
If you want static typing right now, you're probably best looking into TypeScript or Flow.
如果你现在想要静态类型,你可能最好看看 TypeScript 或 Flow。
回答by Rax Wunter
As an option you can take a look at EsLint plugin https://github.com/yarax/typelint
作为一个选项,您可以查看 EsLint 插件https://github.com/yarax/typelint
It's not static check, but optional. The benefit of TypeLint is using already existing app data to build and use types automatically, unlike for example TypeScript or Flow, where you have to describe complex types by yourself.
它不是静态检查,而是可选的。TypeLint 的好处是使用现有的应用程序数据来自动构建和使用类型,这与 TypeScript 或 Flow 不同,您必须自己描述复杂类型。
回答by NagyI
Althought it's not pure ES6, Google's AtScript extending ES6 with type annotations and compiles into valid ES6 code once the compiler gets public: AtScript primer
尽管它不是纯 ES6,但 Google 的 AtScript 使用类型注释扩展了 ES6,并在编译器公开后编译为有效的 ES6 代码:AtScript 入门