{
"compilerOptions": {
"target": "es5", (1)
"module": "commonjs",
"moduleResolution": "node",å
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true
}
}
Typescript
Goals
In this chapter we will explain what Typescript is and how we can code a web application using Typescript.
Learning Outcomes
-
Difference between Typescript and Javascript.
-
How we convert from Typescript into Javascript.
Typescript vs Javascript
From the previous chapter we named the file we are going to type in our code script.ts
The reason the file ends in .ts
instead of .js
is that Angular 2 is written in a superset of Javascript called Typescript.
Typescript is the ES6 version of Javascript plus a few other Typescript only features which Angular 2 needs in order to work.
You can write Angular 2 applications in either Typescript, ES6 or even ES5 Javascript.
However Angular 2 itself is written in Typescript, most examples on the web are written in Typescript, most Angular 2 jobs require you to write Typescript so this book will be teaching in Typescript.
Transpilation
Browsers don’t support Typescript. Browsers barely support ES6 Javascript. So how can we write our code in Typescript?
We use something called a transpiler which converts from one language to another.
We can write in Typescript and have a transpiler convert to ES6 or ES5.
Tip
Since most browsers don’t support ES6 features yet we are going to transpile our Typescript into ES5.
Note
If we look at the tsconfig.json
file in our demo plunker we can see there are a few settings we are using to convert Typescript into Javascript.
-
As you can see the target is set to ES5.
Summary
-
Typescript is just javascript with a few more advanced features.å
-
Browser can’t run Typescript so we first need to Transpile it into Javascript.
-
The most common version of javascript is currently ES5 so we transpile Typescript into ES5 Javascript.