Proposal static type constraints features

YU HengChun achun.shx at
Sun Jun 3 13:58:13 UTC 2018

我的英文水平太 low, 辅助机器翻译, 看双语版吧. __O_O__

To avoid misunderstanding caused by machine translation, I use bilingual.

> is already valid syntax in ECMAScript, and you can't change its semantics willy-nilly

首先, 这个提案要求更改语义才能实现静态类型. 如果这是绝对不允许的, 那这个提案应该被拒绝.
First of all, This proposal requires changing the semantics to achieve static typing.
If this is absolutely not allowed, then this proposal should be rejected.

> How do you ….. errors?

过去可能有人这样用, 但是, 谁写代谁负责, 即便这个提案不被通过, 该发生的错误还是会发生.
Someone may have used it in the past, but whoever writer  is responsible for it. Even if this proposal is not passed, the mistake will still happen.

> Also ….. constranints?

The default value is still the original standard.

类型约束是我的提案, 可以附加到默认值之前.
The type constraint is my proposal and can be appended to the default value.

其实他们是独立的两部分, 只是用运算符 '||' 把他们合法的连接起来.
They are two separate parts, and they are legally connected using operator '||' for backward compatibility.

> which means … do something like?

This proposal just describes how to write static type constraints.

现实中由编译器负责把静态类型约束部分抽离出来, 不参与默认值运算.
In reality, the compiler is responsible for pulling out the static type constraint part, Does not participate in the default operation.

这个提案和 asm.js 的做法非常相似, 并且增大了使用范围.
This proposal is very similar to the asm.js approach and increases the scope of use.

不只是在初始化时标记内建类型, 更广泛的应用于所有定义的类型, 所有初始化和声明.
Not only is the built-in type flagged at initialization, 
it is more widely applied to all defined types, all initializations and declarations.

E.g: asm.js

function geometricMean(start, end) {
  start = start|0; // start has type int
  end = end|0;     // end has type int
  return +exp(+logSum(start, end) / +((end - start)|0));


import types form 'your-types';
function geometricMean(start=void(, end=void( {
  return +exp(+logSum(start, end) / +((end - start)|0));

If the proposal passes, then:

geometricMean('0','1'); // Throws type error

More information about the es-discuss mailing list