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
{{ message }}
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
Copy file name to clipboardExpand all lines: pages/Basic Types.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -214,7 +214,7 @@ Once again, more on union types later on.
214
214
# Never
215
215
216
216
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;
218
218
Variables also acquire the type `never` when narrowed by any type guards that can never be true.
219
219
220
220
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).
Copy file name to clipboardExpand all lines: pages/Classes.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Introduction
2
2
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.
4
4
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.
5
5
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.
Copy file name to clipboardExpand all lines: pages/Namespaces and Modules.md
+2-1Lines changed: 2 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -114,4 +114,5 @@ Here's a revised example:
114
114
## Trade-offs of Modules
115
115
116
116
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`.
Copy file name to clipboardExpand all lines: pages/Variable Declarations.md
+78-15Lines changed: 78 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -475,7 +475,7 @@ function f([first, second]: [number, number]) {
475
475
f(input);
476
476
```
477
477
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 `...`:
479
479
480
480
```ts
481
481
let [first, ...rest] = [1, 2, 3, 4];
@@ -506,7 +506,7 @@ let o = {
506
506
b: 12,
507
507
c: "bar"
508
508
}
509
-
let {a, b} =o;
509
+
let {a, b} =o;
510
510
```
511
511
512
512
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.
515
515
Like array destructuring, you can have assignment without declaration:
516
516
517
517
```ts
518
-
({a, b} = {a: "baz", b: 101});
518
+
({a, b} = {a: "baz", b: 101});
519
519
```
520
520
521
521
Notice that we had to surround this statement with parentheses.
522
522
JavaScript normally parses a `{` as the start of block.
523
523
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
+
524
532
### Property renaming
525
533
526
534
You can also give different names to properties:
527
535
528
536
```ts
529
-
let {a: newName1, b: newName2} =o;
537
+
let {a: newName1, b: newName2} =o;
530
538
```
531
539
532
540
Here the syntax starts to get confusing.
@@ -542,16 +550,16 @@ Confusingly, the colon here does *not* indicate the type.
542
550
The type, if you specify it, still needs to be written after the entire destructuring:
543
551
544
552
```ts
545
-
let {a, b}: {a:string, b:number} =o;
553
+
let {a, b}: {a:string, b:number} =o;
546
554
```
547
555
548
556
### Default values
549
557
550
558
Default values let you specify a default value in case a property is undefined:
551
559
552
560
```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;
555
563
}
556
564
```
557
565
@@ -563,8 +571,8 @@ Destructuring also works in function declarations.
563
571
For simple cases this is straightforward:
564
572
565
573
```ts
566
-
typeC= {a:string, b?:number}
567
-
function f({a, b}:C):void {
574
+
typeC= {a:string, b?:number}
575
+
function f({a, b}:C):void {
568
576
// ...
569
577
}
570
578
```
@@ -573,26 +581,81 @@ But specifying defaults is more common for parameters, and getting defaults righ
573
581
First of all, you need to remember to put the type before the default value.
574
582
575
583
```ts
576
-
function f({a, b} = {a: "", b: 0}):void {
584
+
function f({a, b} = {a: "", b: 0}):void {
577
585
// ...
578
586
}
579
-
f(); // ok, default to {a: "", b: 0}
587
+
f(); // ok, default to {a: "", b: 0}
580
588
```
581
589
582
590
Then, you need to remember to give a default for optional properties on the destructured property instead of the main initializer.
583
591
Remember that `C` was defined with `b` optional:
584
592
585
593
```ts
586
-
function f({a, b=0} = {a: ""}):void {
594
+
function f({a, b=0} = {a: ""}):void {
587
595
// ...
588
596
}
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
591
599
f({}) // error, 'a' is required if you supply an argument
592
600
```
593
601
594
602
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.
596
604
This is especially true with deeply nested destructuring, which gets *really* hard to understand even without piling on renaming, default values, and type annotations.
597
605
Try to keep destructuring expressions small and simple.
598
606
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`.
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
+
moduleMyControllers {
11
+
interfaceZooScopeextendsng.IScope {
12
+
animals:Animal[];
13
+
}
14
+
exportclassZooController {
15
+
// Used to be an error (cannot expose ZooScope), but now is only
0 commit comments