javascript - TypeScript 与 ES6 (ECMAScript 2015) 的兼容性如何(将如何)

标签 javascript typescript ecmascript-6

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

我的 TypeScript 编译器 (1.6.2) 提示以下代码:

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

error TS2339: Property 'EPSILON' does not exist on type 'NumberConstructor'.

打字有问题还是 TypeScript 不是(还)真的是 ES6 的超集?

我还没有尝试过诸如 MapWeakMap、Promises、Generators 等尖端技术……

TypeScript 是稍微落后于 ES6 还是在朝另一个方向发展?我应该通过 Babel 运行 TypeScript 编译器输出吗?

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

最佳答案

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

I thought TypeScript is (basically) ECMAScript 6 (aka. 2015) with additional type annotations.

TypeScript 是带有附加注释的 ECMAScript。 ES6 中的一些特性,如类、剩余参数、lambda、for oflet,在 ES5 或 ES3 中有一个干净的等效代码,编译器可以生成一些代码这些目标。但是新的 API 需要填充,并且编译器不会添加任何运行时库(与 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。您会找到一个接口(interface) 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;
}

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

关于javascript - TypeScript 与 ES6 (ECMAScript 2015) 的兼容性如何(将如何),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32967380/

相关文章:

javascript - Titan 地理定位在 Android 模拟器中不起作用

javascript - 在 javascript 中规范化局部声明是否重要

javascript - 在没有 Javascript 的情况下滚动可滚动的 DIV?

node.js - 如何将自定义css和js添加到angular 4

generics - Typescript 中的通用工厂参数

typescript - 如何在 typescript 中使交叉点类型可选

javascript - 引用-这一行在 ecmascript 中意味着什么?

javascript - 如何防止模板文字中的条件呈现 false

javascript - 使用 Backbone 路由器回调突出显示所选项目

javascript - 静态类属性不适用于 Babel