Skip to content

Commit 57a0c7c

Browse files
authored
Merge pull request #1 from basarat/master
pull upstream
2 parents b9c8cf3 + 446b677 commit 57a0c7c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+743
-215
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ I've been looking at the issues that turn up commonly when people start using Ty
2727
* Thank you for writing TypeScript Deep Dive. I have learned so much. [link](https://twitter.com/buctwbzs/status/857198618704355328?refsrc=email&s=11)
2828
* Loving @basarat's @typescriptlang online book basarat.gitbooks.io/typescript/# loaded with great recipes! [link](https://twitter.com/ericliprandi/status/857608837309677568)
2929
* Microsoft doc is great already, but if want to "dig deeper" into TypeScript I find this book of great value [link](https://twitter.com/caludio/status/876729910550831104)
30+
* Thanks, this is a great book 🤓🤓 [link](https://twitter.com/jjwonmin/status/885666375548547073)
31+
* Deep dive to typescript is awesome in so many levels. i find it very insightful. Thanks [link](https://twitter.com/orenmizr/status/891083492787970053)
3032

3133
## Get Started
3234
If you are here to read the book online [get started](http://basarat.gitbooks.io/typescript/content/docs/getting-started.html).

SUMMARY.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
* [Getting Started](docs/getting-started.md)
44
* [Why TypeScript](docs/why-typescript.md)
55
* [JavaScript](docs/javascript/recap.md)
6-
* [Awful](docs/javascript/awful.md)
6+
* [Equality](docs/javascript/equality.md)
7+
* [References](docs/javascript/references.md)
8+
* [Null vs. Undefined](docs/javascript/null-undefined.md)
9+
* [this](docs/javascript/this.md)
710
* [Closure](docs/javascript/closure.md)
11+
* [Number](docs/javascript/number.md)
812
* [Future JavaScript Now](docs/future-javascript.md)
913
* [Classes](docs/classes.md)
1014
* [Classes Emit](docs/classes-emit.md)
1115
* [Classes Super](docs/classes-super.md)
12-
* [Classes Extensibility](docs/classes-extensibility-wip.md)
1316
* [Arrow Functions](docs/arrow-functions.md)
1417
* [Rest Parameters](docs/rest-parameters.md)
1518
* [let](docs/let.md)
@@ -31,7 +34,8 @@
3134
* [File Module Details](docs/project/external-modules.md)
3235
* [globals.d.ts](docs/project/globals.md)
3336
* [Namespaces](docs/project/namespaces.md)
34-
* [NodeJS QuickStart](docs/quick/nodejs.md)
37+
* [Dynamic Import Expressions](docs/project/dynamic-import-expressions.md)
38+
* [Node.js QuickStart](docs/quick/nodejs.md)
3539
* [Browser QuickStart](docs/quick/browser.md)
3640
* [TypeScript's Type System](docs/types/type-system.md)
3741
* [JS Migration Guide](docs/types/migrating.md)
@@ -43,6 +47,7 @@
4347
* [Enums](docs/enums.md)
4448
* [`lib.d.ts`](docs/types/lib.d.ts.md)
4549
* [Functions](docs/types/functions.md)
50+
* [Callable](docs/types/callable.md)
4651
* [Type Assertion](docs/types/type-assertion.md)
4752
* [Freshness](docs/types/freshness.md)
4853
* [Type Guard](docs/types/typeGuard.md)
@@ -82,7 +87,7 @@
8287
* [Build Toggles](docs/tips/build-toggles.md)
8388
* [Barrel](docs/tips/barrel.md)
8489
* [Create Arrays](docs/tips/create-arrays.md)
85-
* [Promise Safety](docs/tips/promise-safety.md)
90+
* [Typesafe Event Emitter](docs/tips/typed-event.md)
8691
* [StyleGuide](docs/styleguide/styleguide.md)
8792
* [Common Errors](docs/errors/main.md)
8893
* [TypeScript Compiler Internals](docs/compiler/overview.md)

book.json

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
{
2-
"gitbook": ">=3.2.1",
3-
"plugins": ["edit-link", "github"],
2+
"plugins": ["edit-link", "github", "adsense","header"],
43
"pluginsConfig": {
4+
"layout": {
5+
"headerPath" : "header.html"
6+
},
7+
"lunr": {
8+
"ignoreSpecialCharacters": true
9+
},
510
"edit-link": {
611
"base": "https://github.com/basarat/typescript-book/tree/master",
712
"label": "Edit This Page"
813
},
914
"github": {
1015
"url": "https://github.com/basarat/typescript-book/"
16+
},
17+
"adsense": {
18+
"client": "ca-pub-4656761253552116",
19+
"slot": "2017468453",
20+
"format": "auto",
21+
"element": ".page-inner section",
22+
"position": "bottom"
1123
}
1224
}
1325
}

code/async-await/es5/asyncAwaitES5.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function delay(milliseconds, count) {
4040
}, milliseconds);
4141
});
4242
}
43-
// async function allways return a Promise
43+
// async function always return a Promise
4444
function dramaticWelcome() {
4545
return __awaiter(this, void 0, void 0, function () {
4646
var i, count;

code/async-await/es5/asyncAwaitES5.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function delay(milliseconds: number, count: number): Promise<number> {
66
});
77
}
88

9-
// async function allways return a Promise
9+
// async function always return a Promise
1010
async function dramaticWelcome(): Promise<void> {
1111
console.log("Hello");
1212

code/async-await/es6/asyncAwaitES6.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function delay(milliseconds, count) {
1313
}, milliseconds);
1414
});
1515
}
16-
// async function allways return a Promise
16+
// async function always return a Promise
1717
function dramaticWelcome() {
1818
return __awaiter(this, void 0, void 0, function* () {
1919
console.log("Hello");

code/async-await/es6/asyncAwaitES6.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function delay(milliseconds: number, count: number): Promise<number> {
66
});
77
}
88

9-
// async function allways return a Promise
9+
// async function always return a Promise
1010
async function dramaticWelcome(): Promise<void> {
1111
console.log("Hello");
1212

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
function myApp() {
3+
import(/* webpackChunkName: "momentjs" */ "moment")
4+
.then(function (moment) {
5+
// lazyModule has all of the proper types, autocomplete works,
6+
// type checking works, code references work \o/
7+
var time = moment().format();
8+
console.log("TypeScript >= 2.4.0 Dynamic Import Expression:");
9+
console.log(time);
10+
})
11+
.catch(function (err) {
12+
console.log("Failed to load moment", err);
13+
});
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
function myApp() {
3+
import(/* webpackChunkName: "momentjs" */ "moment")
4+
.then((moment) => {
5+
// lazyModule has all of the proper types, autocomplete works,
6+
// type checking works, code references work \o/
7+
const time = moment().format();
8+
console.log("TypeScript >= 2.4.0 Dynamic Import Expression:");
9+
console.log(time);
10+
})
11+
.catch((err) => {
12+
console.log("Failed to load moment", err);
13+
});
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "dynamic-import-expressions",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "dynamicImportExpression.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "Jose Quinto Zamora - https://blog.josequinto.com",
10+
"license": "ISC",
11+
"devDependencies": {
12+
"typescript": "^2.4.1"
13+
},
14+
"dependencies": {
15+
"moment": "^2.18.1"
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "esnext",
5+
"lib": [
6+
"dom",
7+
"es5",
8+
"scripthost",
9+
"es2015.promise"
10+
],
11+
"strict": true,
12+
"moduleResolution": "node"
13+
},
14+
"files": [
15+
"./dynamicImportExpression.ts"
16+
]
17+
}

code/types/callable.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
export namespace asdfasdfasdfasdflkjasdflkjasdflkjasdflkjasdf {
2+
}
3+
4+
namespace A {
5+
interface ReturnString {
6+
(): string
7+
}
8+
declare const foo: ReturnString;
9+
const bar = foo(); // bar is inferred as a string
10+
}
11+
12+
namespace Complex {
13+
interface Complex {
14+
(foo: string, bar?: number, ...others: boolean[]): number;
15+
}
16+
17+
interface Overloaded {
18+
(foo: string): string
19+
(foo: number): number
20+
}
21+
22+
// example implementation
23+
const overloaded: Overloaded = (foo) => foo;
24+
25+
// example usage
26+
const str = overloaded(''); // str is inferred string
27+
const number = overloaded(123); // num is inferred number
28+
}
29+
30+
namespace Direct {
31+
const overloaded: {
32+
(foo: string): string
33+
(foo: number): number
34+
} = (foo) => foo;
35+
36+
const simple: (foo: number) => string
37+
= (foo) => foo.toString();
38+
39+
interface CallMeWithNewToGetString {
40+
new(): string
41+
}
42+
// Usage
43+
declare const Foo: CallMeWithNewToGetString;
44+
const bar = new Foo(); // bar is inferred to be of type string
45+
}
46+

code/types/freshness/index-signatures.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ module jsland {
138138
}
139139

140140
const failsSilently: NestedCSS = {
141-
colour: 'red', // No error is `colour` is a valid string selector
141+
colour: 'red', // No error as `colour` is a valid string selector
142142
}
143143
}
144144

docs/async-await.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function delay(milliseconds: number, count: number): Promise<number> {
6363
});
6464
}
6565

66-
// async function allways return a Promise
66+
// async function always return a Promise
6767
async function dramaticWelcome(): Promise<void> {
6868
console.log("Hello");
6969

@@ -96,7 +96,7 @@ function delay(milliseconds, count) {
9696
}, milliseconds);
9797
});
9898
}
99-
// async function allways return a Promise
99+
// async function always return a Promise
100100
function dramaticWelcome() {
101101
return __awaiter(this, void 0, void 0, function* () {
102102
console.log("Hello");
@@ -157,7 +157,7 @@ function delay(milliseconds, count) {
157157
}, milliseconds);
158158
});
159159
}
160-
// async function allways return a Promise
160+
// async function always return a Promise
161161
function dramaticWelcome() {
162162
return __awaiter(this, void 0, void 0, function () {
163163
var i, count;

docs/classes-emit.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ After having tutored many people about this I find the following explanation to
6565
1. effect of `new` on `this` inside the called function
6666
1. effect of `new` on `prototype` and `__proto__`
6767

68-
All objects in JavaScript contain a `__proto__` member. This member is often not accessible in older browsers (sometimes documentation refers to this magical property as `[[prototype]]`). It has one objective: If a property is not found on an object during lookup (e.g. `obj.property`) then it is looked up at `obj.__proto__.property`. If it is still not found then `obj.__proto__.__proto__.property` till either: *it is found* or *the latest `.__proto__` itself is null*. This explains why JavaScript is said to support *prototypal inheritance* out of the box. This is shown in the following example, which you can run in the chrome console or nodejs:
68+
All objects in JavaScript contain a `__proto__` member. This member is often not accessible in older browsers (sometimes documentation refers to this magical property as `[[prototype]]`). It has one objective: If a property is not found on an object during lookup (e.g. `obj.property`) then it is looked up at `obj.__proto__.property`. If it is still not found then `obj.__proto__.__proto__.property` till either: *it is found* or *the latest `.__proto__` itself is null*. This explains why JavaScript is said to support *prototypal inheritance* out of the box. This is shown in the following example, which you can run in the chrome console or Node.js:
6969

7070
```ts
7171
var foo = {}

docs/classes-extensibility.md

-10
This file was deleted.

docs/classes.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ As always these modifiers work for both member properties and member functions.
118118
### Abstract
119119
`abstract` can be thought of as an access modifier. We present it separately because opposed to the previously mentioned modifiers it can be on a `class` as well as any member of the class. Having an `abstract` modifier primarily means that such functionality *cannot be directly invoked* and a child class must provide the functionality.
120120

121-
* `abstract` **classes** cannot be directly instantiated. Instead the user must create some `class` that inherit from the `abstract class`.
121+
* `abstract` **classes** cannot be directly instantiated. Instead the user must create some `class` that inherits from the `abstract class`.
122122
* `abstract` **members** cannot be directly accessed and a child class must provide the functionality.
123123

124124
### Constructor is optional

docs/compiler/ast-trivia.md

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ For leading and ending comments in a file:
1212
* The first token in the source file gets all the initial trivia.
1313
* The last sequence of trivia in the file is tacked onto the end-of-file token, which otherwise has zero width.
1414

15-
The first token in the source file gets all the initial trivia, and the last sequence of trivia in the file is tacked onto the end-of-file token, which otherwise has zero width.
16-
1715
#### Trivia APIs
1816
For most basic uses, comments are the "interesting" trivia. The comments that belong to a Node can be fetched through the following functions:
1917

docs/compiler/program.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Its interaction mechanism with the OE:
88

99
`Program` *-uses->* `CompilerHost` *-uses->* `System`
1010

11-
The reason for having a `CompilerHost` as a point of indirection is that it allows it's interface to be more finely tuned for `Program` needs and not bother with OE needs (e.g. the `Program` doesn't care about `fileExists` a function provided by `System`).
11+
The reason for having a `CompilerHost` as a point of indirection is that it allows its interface to be more finely tuned for `Program` needs and not bother with OE needs (e.g. the `Program` doesn't care about `fileExists` a function provided by `System`).
1212

1313
There are other users of `System` as well (e.g. tests).
1414

docs/const.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,4 @@ foo.bar = 456; // Allowed!
6262
console.log(foo); // { bar: 456 }
6363
```
6464

65-
For this reason I recommend using `const` with literals or immutable data structures.
65+
For this reason I recommend using `const` with primitives or immutable data structures.

docs/errors/main.md

+19
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,25 @@ Sample:
1919
2020
Checkout the [section on modules][modules].
2121

22+
## Catch clause variable cannot have a type annotation
23+
Sample:
24+
```js
25+
try { something(); }
26+
catch (e: Error) { // Catch clause variable cannot have a type annotation
27+
}
28+
```
29+
TypeScript is protecting you from JavaScript code in the wild being wrong. Use a type guard instead:
30+
```js
31+
try { something(); }
32+
catch (e) {
33+
if (e instanceof Error){
34+
// Here you go.
35+
}
36+
}
37+
```
38+
39+
40+
2241
## For search indexing
2342
You can ignore reading this. This section is for search engine indexing.
2443

docs/future-javascript.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Future JavaScript: Now
2-
One of the main selling points of TypeScript is that it allows you to use a bunch of features from ES6 and beyond in current (ES3 and 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.
2+
One of the main selling points of TypeScript is that it allows you to use a bunch of features from ES6 and beyond in current (ES3 and ES5 level) JavaScript engines (like current browsers and Node.js). Here we deep dive into why these features are useful followed by how these features are implemented in TypeScript.
33

44
Note: Not all of these features are slated for immediate addition to JavaScript but provide great utility to your code organization and maintenance. Also note that you are free to ignore any of the constructs that don't make sense for your project, although you will end up using most of them eventually ;)

docs/generators.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
> NOTE: You cannot use generators in TypeScript in a meaningful way (the ES5 emitter is in progress). However that will change soon so we still have this chapter.
44
5-
`function *` is the syntax used to create a *generator function*. Calling a generator function returns a *generator object*. There are two key motivations behind generator functions. The generator object just follows the [iterator][iterator] interface (i.e. the `next`, `return` and `throw` functions).
5+
`function *` is the syntax used to create a *generator function*. Calling a generator function returns a *generator object*. The generator object just follows the [iterator][iterator] interface (i.e. the `next`, `return` and `throw` functions).
6+
7+
There are two key motivations behind generator functions:
68

79
### Lazy Iterators
810

0 commit comments

Comments
 (0)