首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeScript与ES6的兼容程度(ECMAScript 2015)

TypeScript与ES6的兼容程度(ECMAScript 2015)
EN

Stack Overflow用户
提问于 2015-10-06 10:15:11
回答 2查看 2.9K关注 0票数 5

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

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

代码语言:javascript
复制
if (calc.distance > Number.EPSILON) {
    ...
}

错误TS2339:属性'EPSILON‘在'NumberConstructor’类型中不存在。

类型是否有问题,还是TypeScript还没有真正成为ES6的超集?

我还没试过像MapWeakMap,允诺,生成器,.

TypeScript是稍微落后于ES6,还是走到了另一个方向?我应该通过Babel运行TypeScript编译器输出吗?

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-06 10:31:09

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

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

TypeScript是带有附加注释的ECMAScript。ES6的一些特性,如类、rest参数、lambdas、for oflet,在ES5或ES3中都有相当的代码,编译器可以为这些目标生成一些代码。但是新的API需要填充,编译器不添加任何运行时库(与Babel或Traceur不同)。

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

对于目标ES3和ES5,您必须: 1/加载一个多填充(就像这个)和2/添加TypeScript编译器的定义。

查找TypeScript API的ES6定义

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

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

代码语言:javascript
复制
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;
}

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

票数 5
EN

Stack Overflow用户

发布于 2017-07-23 11:41:54

合法解决办法:

代码语言:javascript
复制
interface INumber {
    EPSILON: any
}
declare var Number: INumber;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32967380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档