I thought TypeScript is (basically) ECMAScript 6 (aka. 2015) with additional type annotations.
My TypeScript piler (1.6.2) plains about following code:
if (calc.distance > Number.EPSILON) {
...
}
error TS2339: Property 'EPSILON' does not exist on type 'NumberConstructor'.
Is there a problem with the typings or is TypeScript not (yet) really a superset of ES6?
I haven't tried such cutting edge things like Map
, WeakMap
, Promises, Generators, ...
Is TypeScript just a little behind ES6 or maybe walks in another direction? Should I run TypeScript piler output through Babel?
Just started with TypeScript and I don't want to back the wrong horse.
I thought TypeScript is (basically) ECMAScript 6 (aka. 2015) with additional type annotations.
My TypeScript piler (1.6.2) plains about following code:
if (calc.distance > Number.EPSILON) {
...
}
error TS2339: Property 'EPSILON' does not exist on type 'NumberConstructor'.
Is there a problem with the typings or is TypeScript not (yet) really a superset of ES6?
I haven't tried such cutting edge things like Map
, WeakMap
, Promises, Generators, ...
Is TypeScript just a little behind ES6 or maybe walks in another direction? Should I run TypeScript piler output through Babel?
Just started with TypeScript and I don't want to back the wrong horse.
Share Improve this question asked Oct 6, 2015 at 10:15 hgoeblhgoebl 13k9 gold badges51 silver badges74 bronze badges 2- 2 TypeScript is indeed not yet really a superset of ES6, lots of ES6 features (modules for example) are not yet ready in TS. – Benjamin Gruenbaum Commented Oct 6, 2015 at 10:33
- @BenjaminGruenbaum Modules are ready in TypeScript (with the ES6 target), not ready in JavaScript VM. – Paleo Commented Oct 6, 2015 at 12:28
2 Answers
Reset to default 6Why Number.EPSILON
doesn't exist for the targets ES3 and ES5
I thought TypeScript is (basically) ECMAScript 6 (aka. 2015) with additional type annotations.
TypeScript is ECMAScript with additional annotations. Some features from ES6, like classes, rest parameters, lambdas, for of
, let
, have a clean equivalent code in ES5 or ES3, and the piler can generate some code for these targets. But new API need to be polyfilled, and the piler doesn't add any runtime library (unlike Babel or Traceur).
Your code pile with the target 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 piler.
Find the TypeScript definition of an ES6 API
So, you need to find the definitions. Here is a tip to find definitions for ES6 API.
All the definitions for ES6 API are already defined in the piler. We can use them. In your node directory, open the file lib/node_modules/typescript/lib/lib.es6.d.ts
. Then, search for EPSILON
. You'll find an interface NumberConstructor
. Below is its code (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;
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744749080a4591482.html
评论列表(0条)