简体   繁体   English

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

[英]How compatible is (will be) TypeScript to ES6 (ECMAScript 2015)

I thought TypeScript is (basically) ECMAScript 6 (aka. 2015) with additional type annotations. 我认为TypeScript(基本上)是ECMAScript 6(又名2015),附带了类型注释。

My TypeScript compiler (1.6.2) complains about following code: 我的TypeScript编译器(1.6.2)抱怨以下代码:

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

error TS2339: Property 'EPSILON' does not exist on type 'NumberConstructor'. 错误TS2339:'NumberConstructor'类型中不存在属性'EPSILON'。

Is there a problem with the typings or is TypeScript not (yet) really a superset of ES6? 是否存在打字问题或者TypeScript(还)是否真的是ES6的超集?

I haven't tried such cutting edge things like Map , WeakMap , Promises, Generators, ... 我没有尝试过像MapWeakMap ,Promises,Generators这样的尖端事物......

Is TypeScript just a little behind ES6 or maybe walks in another direction? TypeScript只是稍微落后于ES6,还是走向另一个方向? Should I run TypeScript compiler output through Babel? 我应该通过Babel运行TypeScript编译器输出吗?

Just started with TypeScript and I don't want to back the wrong horse. 刚开始使用TypeScript,我不想背错马。

Why Number.EPSILON doesn't exist for the targets ES3 and ES5 为什么目标ES3和ES5不存在Number.EPSILON

I thought TypeScript is (basically) ECMAScript 6 (aka. 2015) with additional type annotations. 我认为TypeScript(基本上)是ECMAScript 6(又名2015),附带了类型注释。

TypeScript is ECMAScript with additional annotations. TypeScript是带有附加注释的ECMAScript。 Some features from ES6, like classes, rest parameters, lambdas, for of , let , have a clean equivalent code in ES5 or ES3, and the compiler can generate some code for these targets. ES6的一些功能,如类,休息参数,lambdas, for oflet ,在ES5或ES3中有一个干净的等效代码,编译器可以为这些目标生成一些代码。 But new API need to be polyfilled, and the compiler doesn't add any runtime library (unlike Babel or Traceur). 但是新的API需要进行polyfilled,并且编译器不会添加任何运行时库(与Babel或Traceur不同)。

Your code compile with the target ES6. 您的代码使用目标ES6进行编译。

With the targets ES3 and ES5, you have to: 1/ load a polyfill ( like this one ) and 2/ add the definitions for the TypeScript compiler. 使用目标ES3和ES5,您必须:1 /加载polyfill( 如此 )和2 /添加TypeScript编译器的定义。

Find the TypeScript definition of an ES6 API 查找ES6 API的TypeScript定义

So, you need to find the definitions. 所以,你需要找到定义。 Here is a tip to find definitions for ES6 API. 以下是查找ES6 API定义的提示。

All the definitions for ES6 API are already defined in the compiler. ES6 API的所有定义都已在编译器中定义。 We can use them. 我们可以使用它们。 In your node directory, open the file lib/node_modules/typescript/lib/lib.es6.d.ts . 在节点目录中,打开文件lib/node_modules/typescript/lib/lib.es6.d.ts Then, search for EPSILON . 然后,搜索EPSILON You'll find an interface NumberConstructor . 你会找到一个接口NumberConstructor Below is its code (TS 1.6.2): 以下是其代码(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;
}

Just add this code in your project. 只需在项目中添加此代码即可。

legitimate solution: 合法解决方案

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM