TypeScript对ES6(ECMAScript 2015)有多兼容?

5

我认为 TypeScript 基本上是带有附加类型注释的 ECMAScript 6 (即2015年版)。

我的 TypeScript 编译器(1.6.2)抱怨以下代码:

if (calc.distance > Number.EPSILON) {
    ...
}

错误 TS2339: 类型 'NumberConstructor' 上不存在属性 'EPSILON'。

这是类型定义的问题,还是 TypeScript 还不是 ES6 的超集?

我还没有尝试像 MapWeakMap、Promises、Generators 等这样前沿的技术。

TypeScript 是不是稍微落后于 ES6 或者走了另一条路?我应该将 TypeScript 编译器的输出通过 Babel 进行转换吗?

我刚开始接触 TypeScript,不想赌错方向。


2
TypeScript 确实还不是 ES6 的真正超集,许多 ES6 特性(例如模块)在 TypeScript 中尚未准备好。 - Benjamin Gruenbaum
@BenjaminGruenbaum TypeScript中的模块已经准备好了(使用ES6目标),但在JavaScript虚拟机中还没有准备好。 - Paleo
2个回答

6

为什么ES3和ES5目标不存在Number.EPSILON

我以为TypeScript基本上就是带有附加类型注释的ECMAScript 6(又称2015年版)。

TypeScript是带有附加注释的ECMAScript。ES6中的一些功能,如类、剩余参数、lambda表达式、for oflet在ES5或ES3中都有清晰的等效代码,并且编译器可以为这些目标生成一些代码。但是新的API需要进行polyfill,编译器不会添加任何运行时库(不像Babel或Traceur)。

您的代码使用ES6目标编译。

对于ES3和ES5目标,您需要:1/加载一个polyfill(例如此)和2/添加TypeScript编译器的定义。

查找ES6 API的TypeScript定义

因此,您需要找到这些定义。以下是查找ES6 API定义的提示。

所有ES6 API的定义已经在编译器中定义。我们可以使用它们。在您的node目录中,打开文件lib/node_modules/typescript/lib/lib.es6.d.ts。然后搜索EPSILON。您将找到一个接口NumberConstructor。下面是其代码(TS 1.6.2):

interface NumberConstructor {
    /**
      * The value of Number.EPSILON is the difference between 1 and the smallest value greater than 1
      * that is representable as a Number value, which is approximately:
      * 2.2204460492503130808472633361816 x 10‍−‍16.
      */
    EPSILON: number;

    /**
      * Returns true if passed value is finite.
      * Unlike the global isFininte, Number.isFinite doesn't forcibly convert the parameter to a
      * number. Only finite values of the type number, result in true.
      * @param number A numeric value.
      */
    isFinite(number: number): boolean;

    /**
      * Returns true if the value passed is an integer, false otherwise.
      * @param number A numeric value.
      */
    isInteger(number: number): boolean;

    /**
      * Returns a Boolean value that indicates whether a value is the reserved value NaN (not a
      * number). Unlike the global isNaN(), Number.isNaN() doesn't forcefully convert the parameter
      * to a number. Only values of the type number, that are also NaN, result in true.
      * @param number A numeric value.
      */
    isNaN(number: number): boolean;

    /**
      * Returns true if the value passed is a safe integer.
      * @param number A numeric value.
      */
    isSafeInteger(number: number): boolean;

    /**
      * The value of the largest integer n such that n and n + 1 are both exactly representable as
      * a Number value.
      * The value of Number.MIN_SAFE_INTEGER is 9007199254740991 2^53 − 1.
      */
    MAX_SAFE_INTEGER: number;

    /**
      * The value of the smallest integer n such that n and n − 1 are both exactly representable as
      * a Number value.
      * The value of Number.MIN_SAFE_INTEGER is −9007199254740991 (−(2^53 − 1)).
      */
    MIN_SAFE_INTEGER: number;

    /**
      * Converts a string to a floating-point number.
      * @param string A string that contains a floating-point number.
      */
    parseFloat(string: string): number;

    /**
      * Converts A string to an integer.
      * @param s A string to convert into a number.
      * @param radix A value between 2 and 36 that specifies the base of the number in numString.
      * If this argument is not supplied, strings with a prefix of '0x' are considered hexadecimal.
      * All other strings are considered decimal.
      */
    parseInt(string: string, radix?: number): number;
}

只需将此代码添加到您的项目中即可。


1
我认为将接口复制到代码中并不是添加默认类型的正确方式。无论如何,是否有可能包含默认类型呢? - Manuel
@Manuel 是的,自TS 2.0以来就可以了。请参见此处的选项 --lib [https://www.typescriptlang.org/docs/handbook/compiler-options.html]。我会尽快编辑我的答案。 - Paleo

0

合法的解决方案:

interface INumber {
    EPSILON: any
}
declare var Number: INumber;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接