Skip to content

Commit e74908c

Browse files
authored
Merge pull request #2 from basarat/master
update from upstream
2 parents 57a0c7c + 06f94e0 commit e74908c

File tree

104 files changed

+2421
-932
lines changed

Some content is hidden

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

104 files changed

+2421
-932
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
.alm
1+
# Mac
22
*.DS_Store
33

4+
# IDEs
5+
.alm
6+
.vscode
7+
8+
49
# Node rules:
510
## Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
611
.grunt

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
TypeScript Deep Dive
22
=======
33

4-
I've been looking at the issues that turn up commonly when people start using TypeScript. This is based on the lessons from [StackOverflow](http://stackoverflow.com/tags/typescript/topusers) / [DefinitelyTyped](https://github.com/DefinitelyTyped/) and general engagement with the [TypeScript community](https://github.com/TypeStrong/). You can [follow for updates](https://twitter.com/basarat) and [don't forget to ★ on Github](https://github.com/basarat/typescript-book) 🌹
4+
I've been looking at the issues that turn up commonly when people start using TypeScript. This is based on the lessons from [Stack Overflow](http://stackoverflow.com/tags/typescript/topusers) / [DefinitelyTyped](https://github.com/DefinitelyTyped/) and general engagement with the [TypeScript community](https://github.com/TypeStrong/). You can [follow for updates](https://twitter.com/basarat) and [don't forget to ★ on GitHub](https://github.com/basarat/typescript-book) 🌹
55

66
## Reviews
77

@@ -22,20 +22,27 @@ I've been looking at the issues that turn up commonly when people start using Ty
2222
* Guyz excellent book on Typescript(@typescriptlang) by @basarat ([link](https://twitter.com/deeinlove/status/813245965507260417))
2323
* Leaning on the legendary @basarat's "TypeScript Deep Dive" book heavily at the moment ([link](https://twitter.com/sitapati/status/814379404956532737))
2424
* numTimesPointedPeopleToBasaratsTypeScriptBook++; ([link](https://twitter.com/brocco/status/814227741696462848))
25-
* A book not only for typescript, a good one for deeper javascript knowledge as well. [link](https://www.gitbook.com/book/basarat/typescript/discussions/59)
25+
* A book not only for typescript, a good one for deeper JavaScript knowledge as well. [link](https://www.gitbook.com/book/basarat/typescript/discussions/59)
2626
* In my new job, we're using @typescriptlang, which I am new to. This is insanely helpful huge thanks, @basarat! [link](https://twitter.com/netchkin/status/855339390566096896)
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)
3030
* Thanks, this is a great book 🤓🤓 [link](https://twitter.com/jjwonmin/status/885666375548547073)
3131
* Deep dive to typescript is awesome in so many levels. i find it very insightful. Thanks [link](https://twitter.com/orenmizr/status/891083492787970053)
32+
* @basarat's intro to @typescriptlang is still one of the best going (if not THE best) [link](https://twitter.com/stevealee/status/953953255968698368)
33+
* This is sweet! So many #typescript goodies! [link](https://twitter.com/pauliescanlon/status/989898852474998784)
3234

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

38+
## Translations
39+
Book is completely free so you can copy paste whatever you want without requiring permission. If you have a translation you want me to link here. [Send a PR](https://github.com/basarat/typescript-book/edit/master/README.md).
40+
* [Filipino](https://github.com/themarshann/typescript-book-fil)
41+
* [Italian](https://github.com/TizioFittizio/typescript-book)
42+
3643
## Other Options
3744
You can also download one of the following:
38-
* [EPUB for iPad,iPhone,Mac](https://www.gitbook.com/download/epub/book/basarat/typescript)
45+
* [EPUB for iPad, iPhone, Mac](https://www.gitbook.com/download/epub/book/basarat/typescript)
3946
* [PDF for Windows and others](https://www.gitbook.com/download/pdf/book/basarat/typescript)
4047
* [MOBI for Kindle](https://www.gitbook.com/download/mobi/book/basarat/typescript)
4148

SUMMARY.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
* [Future JavaScript Now](docs/future-javascript.md)
1313
* [Classes](docs/classes.md)
1414
* [Classes Emit](docs/classes-emit.md)
15-
* [Classes Super](docs/classes-super.md)
1615
* [Arrow Functions](docs/arrow-functions.md)
1716
* [Rest Parameters](docs/rest-parameters.md)
1817
* [let](docs/let.md)
@@ -61,12 +60,24 @@
6160
* [Index Signatures](docs/types/index-signatures.md)
6261
* [Moving Types](docs/types/moving-types.md)
6362
* [Exception Handling](docs/types/exceptions.md)
63+
* [Mixins](docs/types/mixins.md)
6464
* [JSX](docs/jsx/tsx.md)
65+
* [React](docs/jsx/react.md)
66+
* [Non React JSX](docs/jsx/others.md)
6567
* [Options](docs/options/intro.md)
6668
* [noImplicitAny](docs/options/noImplicitAny.md)
6769
* [strictNullChecks](docs/options/strictNullChecks.md)
70+
* [Errors in TypeScript](docs/errors/main.md)
71+
* [Interpreting Errors](docs/errors/interpreting-errors.md)
72+
* [Common Errors](docs/errors/common-errors.md)
73+
* [Testing](docs/testing/intro.md)
74+
* [Jest](docs/testing/jest.md)
75+
* [Cypress](docs/testing/cypress.md)
76+
* [Tools](docs/tools/intro.md)
77+
* [Prettier](docs/tools/prettier.md)
78+
* [Husky](docs/tools/husky.md)
79+
* [Changelog](docs/tools/changelog.md)
6880
* [TIPs](docs/tips/main.md)
69-
* [Quick Object Return](docs/tips/quickObjectReturn.md)
7081
* [String Based Enums](docs/tips/stringEnums.md)
7182
* [Nominal Typing](docs/tips/nominalTyping.md)
7283
* [Stateful Functions](docs/tips/statefulFunctions.md)
@@ -77,7 +88,6 @@
7788
* [Classes are Useful](docs/tips/classesAreUseful.md)
7889
* [Avoid Export Default](docs/tips/defaultIsBad.md)
7990
* [Limit Property Setters](docs/tips/propertySetters.md)
80-
* [`null` is bad](docs/tips/null.md)
8191
* [`outFile` caution](docs/tips/outFile.md)
8292
* [JQuery tips](docs/tips/jquery.md)
8393
* [static constructors](docs/tips/staticConstructor.md)
@@ -89,7 +99,6 @@
8999
* [Create Arrays](docs/tips/create-arrays.md)
90100
* [Typesafe Event Emitter](docs/tips/typed-event.md)
91101
* [StyleGuide](docs/styleguide/styleguide.md)
92-
* [Common Errors](docs/errors/main.md)
93102
* [TypeScript Compiler Internals](docs/compiler/overview.md)
94103
* [Program](docs/compiler/program.md)
95104
* [AST](docs/compiler/ast.md)

book.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"language": "en",
3+
"author": "Basarat Ali Syed",
4+
"title": "TypeScript Deep Dive",
25
"plugins": ["edit-link", "github", "adsense","header"],
36
"pluginsConfig": {
47
"layout": {
File renamed without changes.

code/errors/errors.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

code/errors/interpreting-errors.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export const module = 123;
2+
3+
type SomethingComplex = {
4+
foo: number,
5+
bar: string
6+
}
7+
function takeSomethingComplex(arg: SomethingComplex) {
8+
}
9+
function getBar(): string {
10+
return 'some bar';
11+
}
12+
13+
//////////////////////////////////
14+
// Example error production
15+
//////////////////////////////////
16+
const fail = {
17+
foo: 123,
18+
bar: getBar
19+
};
20+
21+
takeSomethingComplex(fail); // TS ERROR HAPPENS HERE

code/errors/tsconfig.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
{}
1+
{
2+
"compilerOptions": {
3+
"noEmit": true
4+
}
5+
}

code/types/generics.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module aaa {
77

88
const queue = new Queue();
99
queue.push(0);
10-
queue.push("1"); // Ops a mistake
10+
queue.push("1"); // Oops a mistake
1111

1212
// a developer walks into a bar
1313
console.log(queue.pop().toPrecision(1));
@@ -68,4 +68,4 @@ namespace ddd {
6868
function loadUsers() {
6969
return getJSON<LoadUsersResponse>({ url: 'https://example.com/users' });
7070
}
71-
}
71+
}

docs/arrow-functions.md

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* [Tip: Arrow Function Danger](#tip-arrow-function-danger)
44
* [Tip: Libraries that use `this`](#tip-arrow-functions-with-libraries-that-use-this)
55
* [Tip: Arrow Function inheritance](#tip-arrow-functions-and-inheritance)
6+
* [Tip: Quick object return](#tip-quick-object-return)
67

78
### Arrow Functions
89

@@ -90,7 +91,7 @@ then `this` is going to be the correct calling context (in this example `person`
9091
In fact if you want `this` *to be the calling context* you should *not use the arrow function*. This is the case with callbacks used by libraries like jquery, underscore, mocha and others. If the documentation mentions functions on `this` then you should probably just use a `function` instead of a fat arrow. Similarly if you plan to use `arguments` don't use an arrow function.
9192

9293
#### Tip: Arrow functions with libraries that use `this`
93-
Many libraries do this e.g. `jQuery` iterables (one example http://api.jquery.com/jquery.each/) will use `this` to pass you the object that it is currently iterating over. In this case if you want to access the library passed `this` as well as the surrounding context just use a temp variable like `_self` like you would in the absence of arrow functions.
94+
Many libraries do this e.g. `jQuery` iterables (one example https://api.jquery.com/jquery.each/) will use `this` to pass you the object that it is currently iterating over. In this case if you want to access the library passed `this` as well as the surrounding context just use a temp variable like `_self` like you would in the absence of arrow functions.
9495

9596
```ts
9697
let _self = this;
@@ -101,8 +102,26 @@ something.each(function() {
101102
```
102103

103104
#### Tip: Arrow functions and inheritance
105+
Arrow functions as properties on classes work fine with inheritance:
104106

105-
If you have an instance method as an arrow function then it goes on `this`. Since there is only one `this` such functions cannot participate in a call to `super` (`super` only works on prototype members). You can easily get around it by creating a copy of the method before overriding it in the child.
107+
```ts
108+
class Adder {
109+
constructor(public a: number) {}
110+
add = (b: number): number => {
111+
return this.a + b;
112+
}
113+
}
114+
class Child extends Adder {
115+
callAdd(b: number) {
116+
return this.add(b);
117+
}
118+
}
119+
// Demo to show it works
120+
const child = new Child(123);
121+
console.log(child.callAdd(123)); // 246
122+
```
123+
124+
However, they do not work with the `super` keyword when you try to override the function in a child class. Properties go on `this`. Since there is only one `this` such functions cannot participate in a call to `super` (`super` only works on prototype members). You can easily get around it by creating a copy of the method before overriding it in the child.
106125

107126
```ts
108127
class Adder {
@@ -122,3 +141,26 @@ class ExtendedAdder extends Adder {
122141
}
123142
}
124143
```
144+
145+
### Tip: Quick object return
146+
147+
Sometimes you need a function that just returns a simple object literal. However, something like
148+
149+
```ts
150+
// WRONG WAY TO DO IT
151+
var foo = () => {
152+
bar: 123
153+
};
154+
```
155+
is parsed as a *block* containing a *JavaScript Label* by JavaScript runtimes (cause of the JavaScript specification).
156+
157+
> If that doesn't make sense, don't worry, as you get a nice compiler error from TypeScript saying "unused label" anyways. Labels are an old (and mostly unused) JavaScript feature that you can ignore as a modern GOTO (considered bad by experienced developers 🌹)
158+
159+
You can fix it by surrounding the object literal with `()`:
160+
161+
```ts
162+
// Correct 🌹
163+
var foo = () => ({
164+
bar: 123
165+
});
166+
```

0 commit comments

Comments
 (0)