(将是)TypeScript与ES6的兼容性(ECMAScript 2015)

时间:2015-10-06 10:15:11

标签: javascript typescript ecmascript-6

我认为TypeScript(基本上)是ECMAScript 6(又名2015),带有其他类型的注释。

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

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

错误TS2339:属性' EPSILON'类型' NumberConstructor'。

上不存在

是否存在打字问题,或者TypeScript不是(还)确实是ES6的超集?

我还没有尝试过MapWeakMap,Promises,Generators等最先进的东西......

TypeScript是否稍微落后于ES6,或者可能走向另一个方向?我应该通过Babel运行TypeScript编译器输出吗?

刚开始使用TypeScript,我不想背错马。

2 个答案:

答案 0 :(得分:4)

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

  

我认为TypeScript(基本上)是ECMAScript 6(又名2015),带有其他类型的注释。

TypeScript是带有附加注释的ECMAScript。 ES6的一些功能,如类,休息参数,lambdas,for oflet,在ES5或ES3中有一个干净的等效代码,编译器可以为这些目标生成一些代码。但是新的API需要进行polyfilled,编译器不会添加任何运行时库(与Babel或Traceur不同)。

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

使用目标ES3和ES5,您必须:1 /加载polyfill(like this one)和2 /添加TypeScript编译器的定义。

查找ES6 API的TypeScript定义

所以,你需要找到定义。以下是查找ES6 API定义的提示。

ES6 API的所有定义都已在编译器中定义。我们可以使用它们。在节点目录中,打开文件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 :(得分:0)

合法解决方案:

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