简体   繁体   English

在ECMAScript 6或7中是否支持静态类型?

[英]Is there support for static typing in ECMAScript 6 or 7?

Is there any support for static typing in ECMAScript 6? 在ECMAScript 6中是否支持静态类型? How about ECMAScript 7? ECMAScript 7怎么样?

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. 第七版处于发展的早期阶段,但旨在继续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. 提出的新功能包括承诺/并发,数字和数学增强,警卫和商标(静态类型的替代) ,运算符重载,值类型(类一类数字对象),新记录结构(记录,元组和类型化数组) ,模式匹配和特征。

Which may interest you. 你可能会感兴趣。

Though this isn't part of the ES6 spec, Closure Compiler enforces JSDoc argument type annotations in JavaScript code when using its Advanced compilation level. 虽然这不是ES6规范的一部分,但Closure Compiler在使用其高级编译级别时在JavaScript代码中强制执行JSDoc参数类型注释。 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. 使用注释指定类型注释,以便在开发中忽略它们,但是当您为生产版本构建应用程序时,类型不匹配将导致编译器警告或(可选)编译器错误。

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. 作为额外的好处,JSDoc可以使用相同的语法构建API文档。 So it's also handy if you document your code. 因此,如果您记录代码,它也很方便。

But a warning: for Closure Compiler to do its advanced optimization magic, every engineer on your project has to follow certain strict coding conventions. 但是警告:对于Closure Compiler进行高级优化魔术,项目中的每个工程师都必须遵循某些严格的编码约定。 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. 编译器不能强制执行类型,除非它能够可靠地弄清楚你的代码在做什么,这意味着放弃了一些JavaScript的动态和多余的语法。 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. 大多数流行的JavaScript框架和库都不遵循它们,尽管您有时可以使用Compiler的externs功能解决这个问题。 (jQuery is supported using externs, for example.) (例如,使用externs支持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 after it's been optimized. 我个人甚至不会考虑在Web应用程序上使用此功能,除非它有一个Jenkins构建机器人和接近100%的自动化测试覆盖率,可以在优化对代码运行。 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. 但就个人而言,我认为值得付出努力。

For more information, check out Advanced Compilation and Externs and Annotating JavaScript for the Closure Compiler . 有关更多信息,请查看Closure Compiler的 Advanced Compilation and ExternsAnnotating JavaScript

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, 4 or stage 0 for static typing. 对于ECMAScript 7(ES2016),在静态类型的第1,2,3,40 阶段中没有任何提议。

I've seen a few proposals/ideas for static typing appear on the es-discuss mailing 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。

虽然它不是纯粹的ES6,谷歌的AtScript扩展了带有类型注释的ES6,并在编译器公开后编译成有效的ES6代码: AtScript primer

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不同,您必须自己描述复杂类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM