You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Lovingly called the *fat arrow* (becuase `->` is a thin arrow and `=>` is a fat arrow) and also called a *lambda function* (because of other languages). Another commonly used feature is the fat arrow function `()=>something`. The motivation for a *fat arrow* is:
4
+
1. You don't need to keep typing `function`
5
+
1. I lexically captures the meaning of `this`
6
+
7
+
For a language that claims to be functional, in JavaScript you tend to be typing `function` quite a lot. The fat arrow makes it simple for you to create a function
8
+
```ts
9
+
var inc = (x)=>x+1;
10
+
```
11
+
`this` has traditionally been a pain point in JavaScript. As a wise man once said "I hate JavaScript as it tends to lose the meaning of `this` all too easily". Fat arrows fix it by capturing the meaning of `this` from the surrounding context. Consider this pure JavaScript class:
12
+
13
+
```ts
14
+
function Person(age) {
15
+
this.age=age
16
+
this.growOld=function(){
17
+
this.age++;
18
+
}
19
+
}
20
+
var person =newPerson(1);
21
+
setTimeout(person.growOld,1000);
22
+
23
+
setTimeout(function(){ console.log(person.age); },2000); // 1, should have been 2
24
+
```
25
+
If you run this code in the browser `this` within the function is going to point to `window` because `window` is going to be what executes the `growOld` function. Fix is to use an arrow function:
The reason why this works is the reference to `this` is captured by the arrow function from outside the function body. This is equivalent to the following JavaScript code (which is what you would write yourself if you didn't have TypeScript):
One of the main selling points of TypeScript is that it allows you to use a bunch of features from ES6 in current (ES5 level) JavaScript engines (like current browsers and NodeJS). Here we deep dive into why these features are useful followed by how these features are implemented in TypeScript.
Copy file name to clipboardExpand all lines: docs/why-typescript.md
+72-2
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ We find it best to explain these in separation in first go.
14
14
### Your JavaScript is TypeScript
15
15
TypeScript provides compile time Type safety for your JavaScript code. This is no surprise given its name. The great thing is that the types are completely optional. Your JavaScript code `.js` file can be renamed to a `.ts` file and TypeScript will still give you back valid `.js` equivalent to the original JavaScript file. TypeScript is *intentionally* and strictly a superset of JavaScript with optional Type checking.
16
16
17
-
### Types are Inferred
17
+
### Types can be Implicit
18
18
In order to give you type safety with minimal cost of productivity during new code development. E.g. TypeScript will know that foo is of type `number` below and will give an error on the second line as shown:
The motivation is that if you do stuff like this, in the rest of your code you cannot be certain that `foo` is a `number` or a `string`. Such issues turn up often in large multi-file code bases. We will deep dive into the type inference rules later.
27
27
28
-
### Types can be specified
28
+
### Types can be Explicit
29
29
As we've mentioned before, TypeScript will infer as much as it can safely, however you can use annotations to:
30
30
1. Help along the compiler, and more importantly the next developer who has to read your code (that might be future you!).
31
31
1. Enforce that what the compiler sees is, what you thought it should see. That is your understanding of the code matches an algorithmic analysis of the code (done by the compiler).
@@ -65,9 +65,79 @@ iTakePoint2D(point3D); // extra information okay
65
65
iTakePoint2D({ x: 0 }); // Error: missing information `y`
66
66
```
67
67
68
+
### Type errors do not prevent JavaScript emit
69
+
To make it easy for you to migrate your JavaScript code to TypeScript, even if there are compilation errors, by default TypeScript *will emit valid JavaScript* the best that it can. e.g.
70
+
71
+
```ts
72
+
var foo =123;
73
+
foo='456'; // Error: cannot assign a `string` to a `number`
74
+
```
75
+
76
+
will emit the following js:
77
+
78
+
```ts
79
+
var foo =123;
80
+
foo='456';
81
+
```
82
+
83
+
So you can incrementally upgrade your JavaScript code to TypeScript. This is very different from how many other language compilers work and is there for your ease.
84
+
85
+
### Types can be ambient
86
+
A major design goal of TypeScript was to make it possible for you to safely and easily use existing JavaScript libraries in TypeScript. TypeScript does this by means of *declaration*. TypeScript provides you with a sliding scale of how much or how little effort you want to put in your declarations, the more effort you put the more type safety + code intelligence you get. Note that definitions for most of the popular libraries have already been written for you by the [DefinitelyTyped community](https://github.com/borisyankov/DefinitelyTyped) so for the most purposes:
87
+
88
+
1. The definition already exists
89
+
1. You have a vast list of well reviewed TypeScript declaration templates already available.
90
+
91
+
As a quick example consider a trivial example of [jquery](https://jquery.com/). By default (as expect in good JS code) TypeScript expects you to declare (i.e. use `var` somewhere) before you use a variable
92
+
```ts
93
+
$('.awesome').show(); // Error: cannot find name `$`
94
+
```
95
+
As a quick fix *you can tell TypeScript* that there is indeed something called `$`:
96
+
```ts
97
+
declarevar $:any;
98
+
$('.awesome').show(); // Okay!
99
+
```
100
+
If you want you can build on this basic definition and provide more information to help protect you from errors:
101
+
```ts
102
+
declarevar $:{
103
+
(selector:string)=>any;
104
+
};
105
+
$('.awesome').show(); // Okay!
106
+
$(123).show(); // Error: selector needs to be a string
107
+
```
108
+
109
+
We will discuss the details of creating TypeScript definitions for existing JavaScript in detail later once you know more about TypeScript (e.g. stuff like `interface` and the `any`).
110
+
111
+
## Future JavaScript Now
112
+
TypeScript provides a number of features that are planned in ES6 for current JavaScript engines (that only support ES5 etc). The typescript team is actively adding these features and this list is only going to get bigger over time and we will cover this in its own section. But just as a specimen here is an example of a class:
113
+
114
+
```ts
115
+
classPoint {
116
+
constructor(publicx:number, publicy:number) {
117
+
}
118
+
add(point:Point) {
119
+
returnnewPoint(this.x+point.x, this.y+point.y);
120
+
}
121
+
}
122
+
123
+
var p1 =newPoint(0, 10);
124
+
var p2 =newPoint(10, 20);
125
+
var p3 =p1.add(p2); // {x:10,y:30}
126
+
```
127
+
128
+
and the lovely fat arrow function:
129
+
130
+
```ts
131
+
var inc = (x)=>x+1;
132
+
```
133
+
134
+
### Summary
135
+
In this section we have provided you with the motivation and design goals of TypeScript. With this out of the way we can dig into the nitty gritty details of TypeScript.
68
136
69
137
[](Interfaces are open ended)
70
138
[](Type Inferernce rules)
71
139
[](Cover all the annotations)
140
+
[](Cover all ambients : also that there are no runtime enforcement)
0 commit comments