Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

My TypeScript compiler (1.6.2) complains 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 compiler output through Babel?

Just started with TypeScript and I don't want to back the wrong horse.

like image 778
hgoebl Avatar asked Oct 06 '15 10:10

hgoebl


People also ask

Does TypeScript work with ES6?

TypeScript is a transpiler. Grunt, Gulp, and Babel are some other transpilers to compile the modules. Therefore, TypeScript supports ES6.

Does TypeScript also support ECMAScript?

Features. TypeScript is a language extension that adds features to ECMAScript 6. Additional features include: Type annotations and compile-time type checking.

Does TypeScript use ES6 modules?

TypeScript 1.5 supports ECMAScript 6 (ES6) modules. ES6 modules are effectively TypeScript external modules with a new syntax: ES6 modules are separately loaded source files that possibly import other modules and provide a number of externally accessible exports.

Is ECMAScript the same as TypeScript?

ECMA script 6 is the sixth edition of ECMAScript trademarked scripting language specification defined by ECMA international. TypeScript is a free and open-source pure object-oriented programming language developed and maintained by Microsoft. 2. It does not support all data types.


1 Answers

Why 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 compiler can generate some code for these targets. But new API need to be polyfilled, and the compiler doesn't add any runtime library (unlike Babel or Traceur).

Your code compile 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 compiler.

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 compiler. 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.

like image 150
Paleo Avatar answered Sep 24 '22 17:09

Paleo