-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdestructuring.ts
110 lines (83 loc) · 1.96 KB
/
destructuring.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// --- Destructuring ---
// -- Array Destructing --
{
let input = [1, 2];
let [first, second] = input;
console.log(first); // outputs 1
console.log(second); // outputs 2
// swap variables
[first, second] = [second, first];
}
// parameters to a function
{
let input = [1, 2];
function f([first, second]: [number, number]) {
console.log(first);
console.log(second);
}
f(input);
}
// remaining items
{
let [first, ...rest] = [1, 2, 3, 4];
console.log(first); // outputs 1
console.log(rest); // outputs [ 2, 3, 4 ]
}
{
// ignore trailing elements
let [first] = [1, 2, 3, 4];
console.log(first); // outputs 1
// ignore other elements`
let [, second, , fourth] = [1, 2, 3, 4];
}
// -- Object destructuring --
{
let o = {
a: "foo",
b: 12,
c: "bar"
}
let {a, b} = o;
}
let defaults = {food: "spicy", price: "$$", ambiance: "noisy"};
let search = {food: "rich", ...defaults};
// Object spread also has a couple of other surprising limits.
// It only includes an objects’ own, enumerable properties.
// That means you lose methods when you spread instances of an object:
class CS {
p = 12;
m() {
}
}
let c = new CS();
let clone = {...c};
clone.p; // ok
clone.m(); // error!
// Property renaming
{
let {a: newName1, b: newName2} = o;
}
// Typescript compiler doesn’t allow spreads of type parameters from generic functions.
// Default values
{
function keepWholeObject(wholeObject: { a: string, b?: number }) {
let {a, b = 1001} = wholeObject;
}
}
// -- Function declarations --
{
type C = { a: string, b?: number }
function f2({a, b}: C): void {
// ...
}
function f3({a, b} = {a: "", b: 0}): void {
// ...
}
f3(); // ok, default to {a: "", b: 0}
function f4({a, b = 0} = {a: ""}): void {
// ...
}
f4({a: "yes"}) // ok, default b = 0
f4() // ok, default to {a: ""}, which then defaults b = 0
f4({}) // error, 'a' is required if you supply an argument
}