Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 513588b

Browse files
authored
Merge branch 'master' into updateReactWebpack
2 parents fd0bb44 + 7f18053 commit 513588b

19 files changed

+3048
-58
lines changed

pages/Basic Types.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ Once again, more on union types later on.
214214
# Never
215215

216216
The `never` type represents the type of values that never occur.
217-
For instance, `never` is the return type for a function expression or an arrow function expresssion that always throws an exception or one that never returns;
217+
For instance, `never` is the return type for a function expression or an arrow function expression that always throws an exception or one that never returns;
218218
Variables also acquire the type `never` when narrowed by any type guards that can never be true.
219219

220220
The `never` type is a subtype of, and assignable to, every type; however, *no* type is a subtype of, or assignable to, `never` (except `never` itself).

pages/Classes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Introduction
22

3-
Traditional JavaScript focuses on functions and prototype-based inheritance as the basic means of building up reusable components, but this may feel a bit awkward to programmers more comfortable with an object-oriented approach, where classes inherit functionality and objects are built from these classes.
3+
Traditional JavaScript uses functions and prototype-based inheritance to build up reusable components, but this may feel a bit awkward to programmers more comfortable with an object-oriented approach, where classes inherit functionality and objects are built from these classes.
44
Starting with ECMAScript 2015, also known as ECMAScript 6, JavaScript programmers will be able to build their applications using this object-oriented class-based approach.
55
In TypeScript, we allow developers to use these techniques now, and compile them down to JavaScript that works across all major browsers and platforms, without having to wait for the next version of JavaScript.
66

pages/Compiler Options.md

Lines changed: 17 additions & 14 deletions
Large diffs are not rendered by default.

pages/Decorators.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ function validate<T>(target: any, propertyKey: string, descriptor: TypedProperty
453453
if (!(value instanceof type)) {
454454
throw new TypeError("Invalid type.");
455455
}
456+
set(value);
456457
}
457458
}
458459
```

pages/Module Resolution.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ You should use relative imports for your own modules that are guaranteed to main
4040

4141
A non-relative import can be resolved relative to `baseUrl`, or through path mapping, which we'll cover below.
4242
They can also resolve to [ambient module declarations](./Modules.md#ambient-modules).
43-
Use non-relative paths when importing any of your external dependnecies.
43+
Use non-relative paths when importing any of your external dependencies.
4444

4545
## Module Resolution Strategies
4646

4747
There are two possible module resolution strategies: [Node](#node) and [Classic](#classic).
4848
You can use the `--moduleResolution` flag to specify the module resolution strategy.
49-
The default if not specified is [Node](#node).
49+
If not specified, the default is [Classic](#classic) for `--module AMD | System | ES2015` or [Node](#node) otherwise.
5050

5151
### Classic
5252

@@ -213,13 +213,15 @@ Here is an example for how to specify the `"paths"` property for `jquery`.
213213
```json
214214
{
215215
"compilerOptions": {
216+
"baseUrl": ".", // This must be specified if "paths" is.
216217
"paths": {
217-
"jquery": ["node_modules/jquery/dist/jquery.d.ts"]
218+
"jquery": ["node_modules/jquery/dist/jquery"]
218219
}
220+
}
219221
}
220222
```
221223

222-
Using `"paths"` also allow for more sophisticated mappings including multiple fall back locations.
224+
Using `"paths"` also allows for more sophisticated mappings including multiple fall back locations.
223225
Consider a project configuration where only some modules are available in one location, and the rest are in another.
224226
A build step would put them all together in one place.
225227
The project layout may look like:
@@ -240,15 +242,15 @@ The corresponding `tsconfig.json` would look like:
240242

241243
```json
242244
{
243-
"compilerOptions": {
244-
"baseUrl": ".",
245-
"paths": {
246-
"*": [
247-
"*",
248-
"generated/*"
249-
]
250-
}
245+
"compilerOptions": {
246+
"baseUrl": ".",
247+
"paths": {
248+
"*": [
249+
"*",
250+
"generated/*"
251+
]
251252
}
253+
}
252254
}
253255
```
254256

pages/Namespaces and Modules.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,5 @@ Here's a revised example:
114114
## Trade-offs of Modules
115115

116116
Just as there is a one-to-one correspondence between JS files and modules, TypeScript has a one-to-one correspondence between module source files and their emitted JS files.
117-
One effect of this is that it's not possible to use the `--outFile` compiler switch to concatenate multiple module source files into a single JavaScript file.
117+
One effect of this is that it's not possible to concatenate multiple module source files depending on the module system you target.
118+
For instance, you can't use the `outFile` option while targeting `commonjs` or `umd`, but with TypeScript 1.8 and later, [it's possible](https://www.typescriptlang.org/docs/release-notes/typescript-1.8.html#concatenate-amd-and-system-modules-with---outfile) to use `outFile` when targeting `amd` or `system`.

pages/Variable Declarations.md

Lines changed: 78 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ function f([first, second]: [number, number]) {
475475
f(input);
476476
```
477477

478-
You can create a variable for the remaining items in a list using the syntax `...name`:
478+
You can create a variable for the remaining items in a list using the syntax `...`:
479479

480480
```ts
481481
let [first, ...rest] = [1, 2, 3, 4];
@@ -506,7 +506,7 @@ let o = {
506506
b: 12,
507507
c: "bar"
508508
}
509-
let {a, b} = o;
509+
let { a, b } = o;
510510
```
511511

512512
This creates new variables `a` and `b` from `o.a` and `o.b`.
@@ -515,18 +515,26 @@ Notice that you can skip `c` if you don't need it.
515515
Like array destructuring, you can have assignment without declaration:
516516

517517
```ts
518-
({a, b} = {a: "baz", b: 101});
518+
({ a, b } = { a: "baz", b: 101 });
519519
```
520520

521521
Notice that we had to surround this statement with parentheses.
522522
JavaScript normally parses a `{` as the start of block.
523523

524+
You can create a variable for the remaining items in an object using the syntax `...`:
525+
526+
```ts
527+
let { a, ...passthrough } = o;
528+
let total = passthrough.b + passthrough.c.length;
529+
530+
```
531+
524532
### Property renaming
525533

526534
You can also give different names to properties:
527535

528536
```ts
529-
let {a: newName1, b: newName2} = o;
537+
let { a: newName1, b: newName2 } = o;
530538
```
531539

532540
Here the syntax starts to get confusing.
@@ -542,16 +550,16 @@ Confusingly, the colon here does *not* indicate the type.
542550
The type, if you specify it, still needs to be written after the entire destructuring:
543551

544552
```ts
545-
let {a, b}: {a: string, b: number} = o;
553+
let { a, b }: { a: string, b: number } = o;
546554
```
547555

548556
### Default values
549557

550558
Default values let you specify a default value in case a property is undefined:
551559

552560
```ts
553-
function keepWholeObject(wholeObject: {a: string, b?: number}) {
554-
let {a, b = 1001} = wholeObject;
561+
function keepWholeObject(wholeObject: { a: string, b?: number }) {
562+
let { a, b = 1001 } = wholeObject;
555563
}
556564
```
557565

@@ -563,8 +571,8 @@ Destructuring also works in function declarations.
563571
For simple cases this is straightforward:
564572

565573
```ts
566-
type C = {a: string, b?: number}
567-
function f({a, b}: C): void {
574+
type C = { a: string, b?: number }
575+
function f({ a, b }: C): void {
568576
// ...
569577
}
570578
```
@@ -573,26 +581,81 @@ But specifying defaults is more common for parameters, and getting defaults righ
573581
First of all, you need to remember to put the type before the default value.
574582

575583
```ts
576-
function f({a, b} = {a: "", b: 0}): void {
584+
function f({ a, b } = { a: "", b: 0 }): void {
577585
// ...
578586
}
579-
f(); // ok, default to {a: "", b: 0}
587+
f(); // ok, default to { a: "", b: 0 }
580588
```
581589

582590
Then, you need to remember to give a default for optional properties on the destructured property instead of the main initializer.
583591
Remember that `C` was defined with `b` optional:
584592

585593
```ts
586-
function f({a, b = 0} = {a: ""}): void {
594+
function f({ a, b = 0 } = { a: "" }): void {
587595
// ...
588596
}
589-
f({a: "yes"}) // ok, default b = 0
590-
f() // ok, default to {a: ""}, which then defaults b = 0
597+
f({ a: "yes" }) // ok, default b = 0
598+
f() // ok, default to { a: "" }, which then defaults b = 0
591599
f({}) // error, 'a' is required if you supply an argument
592600
```
593601

594602
Use destructuring with care.
595-
As the previous example demonstrates, anything but the simplest destructuring expressions have a lot of corner cases.
603+
As the previous example demonstrates, anything but the simplest destructuring expression is confusing.
596604
This is especially true with deeply nested destructuring, which gets *really* hard to understand even without piling on renaming, default values, and type annotations.
597605
Try to keep destructuring expressions small and simple.
598606
You can always write the assignments that destructuring would generate yourself.
607+
608+
## Spread
609+
610+
The spread operator is the opposite of destructuring.
611+
It allows you to spread an array into another array, or an object into another object.
612+
For example:
613+
614+
```ts
615+
let first = [1, 2];
616+
let second = [3, 4];
617+
let bothPlus = [0, ...first, ...second, 5];
618+
```
619+
620+
This gives bothPlus the value `[0, 1, 2, 3, 4, 5]`.
621+
Spreading creates a shallow copy of `first` and `second`.
622+
They are not changed by the spread.
623+
624+
You can also spread objects:
625+
626+
```ts
627+
let defaults = { food: "spicy", price: "$$", ambiance: "noisy" };
628+
let search = { ...defaults, food: "rich" };
629+
```
630+
631+
Now `search` is `{ food: "rich", price: "$$", ambiance: "noisy" }`.
632+
Object spreading is more complex than array spreading.
633+
Like array spreading, it proceeds from left-to-right, but the result is still an object.
634+
This means that properties that come later in the spread object overwrite properties that come earlier.
635+
So if we modify the previous example to spread at the end:
636+
637+
```ts
638+
let defaults = { food: "spicy", price: "$$", ambiance: "noisy" };
639+
let search = { food: "rich", ...defaults };
640+
```
641+
642+
Then the `food` property in `defaults` overwrites `food: "rich"`, which is not what we want in this case.
643+
644+
Object spread also has a couple of other surprising limits.
645+
First, it only includes own, enumerable properties.
646+
Basically, that means you lose methods when you spread instances of an object:
647+
648+
```ts
649+
class C {
650+
p = 12;
651+
m() {
652+
}
653+
}
654+
let c = new C();
655+
let clone = { ...c };
656+
clone.p; // ok
657+
clone.m(); // error!
658+
```
659+
660+
Second, the Typescript compiler doesn't allow spreads of type parameters from generic functions.
661+
That feature is expected in future versions of the language.

pages/declaration files/By Example.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,10 @@ declare function getWidget(s: string): Widget[];
114114

115115
> When specifying a greeting, you must pass a `GreetingSettings` object.
116116
> This object has the following properties:
117-
> - greeting: Mandatory string
118-
> - duration: Optional length of time (in milliseconds)
119-
> - color: Optional string, e.g. '#ff00ff'
117+
>
118+
> 1- greeting: Mandatory string
119+
> 2- duration: Optional length of time (in milliseconds)
120+
> 3- color: Optional string, e.g. '#ff00ff'
120121
121122
*Code*
122123

pages/declaration files/Do's and Don'ts.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,19 +143,19 @@ When an earlier overload is "more general" than a later one, the later one is ef
143143

144144
```ts
145145
/* WRONG */
146-
interface Moment {
147-
diff(b: MomentComparable): number;
148-
diff(b: MomentComparable, unitOfTime: string): number;
149-
diff(b: MomentComparable, unitOfTime: string, round: boolean): number;
146+
interface Example {
147+
diff(one: string): number;
148+
diff(one: string, two: string): number;
149+
diff(one: string, two: string, three: boolean): number;
150150
}
151151
```
152152

153153
*Do* use optional parameters whenever possible:
154154

155155
```ts
156156
/* OK */
157-
interface Moment {
158-
diff(b: MomentComparable, unitOfTime?: string, round?: boolean): number;
157+
interface Example {
158+
diff(one: string, two?: string, three?: boolean): number;
159159
}
160160
```
161161

@@ -169,7 +169,7 @@ This code, for example, exposes a bug only when the signature is correctly writt
169169

170170
```ts
171171
function fn(x: (a: string, b: number, c: number) => void) { }
172-
var x: Moment;
172+
var x: Example;
173173
// When written with overloads, OK -- used first overload
174174
// When written with optionals, correctly an error
175175
fn(x.diff);
@@ -180,10 +180,10 @@ Because unspecified parameters appear as `undefined` in JavaScript, it's usually
180180
This code, for example, should be OK under strict nulls:
181181

182182
```ts
183-
var x: Moment;
183+
var x: Example;
184184
// When written with overloads, incorrectly an error because of passing 'undefined' to 'string'
185185
// When written with optionals, correctly OK
186-
x.diff(something, someOtherThing ? undefined : "hour");
186+
x.diff("something", true ? undefined : "hour");
187187
```
188188

189189
## Use Union Types
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Performance Improvements
2+
3+
The 1.1 compiler is typically around 4x faster than any previous release. See [this blog post for some impressive charts.](http://blogs.msdn.com/b/typescript/archive/2014/10/06/announcing-typescript-1-1-ctp.aspx)
4+
5+
# Better Module Visibility Rules
6+
7+
TypeScript now only strictly enforces the visibility of types in modules if the `--declaration` flag is provided. This is very useful for Angular scenarios, for example:
8+
9+
```ts
10+
module MyControllers {
11+
interface ZooScope extends ng.IScope {
12+
animals: Animal[];
13+
}
14+
export class ZooController {
15+
// Used to be an error (cannot expose ZooScope), but now is only
16+
// an error when trying to generate .d.ts files
17+
constructor(public $scope: ZooScope) { }
18+
/* more code */
19+
}
20+
}
21+
```

0 commit comments

Comments
 (0)