The relationship between TypeScript and ES6

Everything:

I'm new to Typescript and ES6, the first thing that bothers me is their relationship, from MSDN:

TypeScript 1.5 adds a number of new ES6 features, including modules, destructuring, distribution, for ... characters, computed properties, let / const, and tagged string patterns.

My confusion (I know there are many posts that say Typescript is a superset of JS):

To dispense this value Typescript just use your own way (a little diff and transpile syntax) to repeat what already exists in ES6 again (only for the purpose of the type)

and

Does this mean that ES6 can basically do everything in TypeScript? and vice versa

+5
source share
3 answers

TypeScript is script code that translates to JavaScript - either in ES5 or ES6 (and ES3 too).

TypeScript 1.5 adds a number of new ES6 features, including modules, destructuring, distribution, for ... characters, computed properties, let / const, and tagged string patterns.

This means that you can use modules for ... and other functions in TypeScript code and the TypeScript compiler, your code is translated into ESx-compatible code that does the same. Take for..of , for example:

TypeScript code:

 for (let t of [1,2,3]) { console.log(t) } 

transferred to ES5 as follows:

 for (var _i = 0, _a = [1, 2, 3]; _i < _a.length; _i++) { var t = _a[_i]; console.log(t); } 

However, if you are targeting ES6, then transpiling is simple:

 for (let t of [1,2,3]) { console.log(t); } 

The same is true for modules, distribution, etc. In each case, TypeScript generates code that behaves the same in ES5, ES6, and ES6 (simplified because this is not always possible).

There is no difference in the expressiveness of TypeScript and ES6. The difference is that the TypeScript compiler helps you with static analysis of your code. Otherwise, no matter what you program in ES6, you can program TypeScript and vice versa.

+4
source

Typescript is a compiler that creates ecmascript. Typescript source is code that supports types, interfaces, super / subclasses and everything you know from other programming languages. As already mentioned, the result is ecmascript, which runs in any browser.

+2
source

ecmascript is the standard specification from which Javascript follows. ES5, ES6, ES7 are versions of this standard specification. Typescript is executed in Javascript. Depending on the version of the node compiler that you have will determine which version of the standard version of Javascript your Typescript will compile.

+1
source

Source: https://habr.com/ru/post/1242029/


All Articles