• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16function destructuringParameters(): void {
17  function drawText({ text = '', position: [x, y] = [0, 0] }): void {
18    // Draw text
19  }
20  drawText({ text: 'Figure 1', position: [10, 20] });
21
22  function print([a, b]): void {
23    console.log(a, b);
24  }
25  print(['Hello', 'Wolrd']);
26
27  const hello = ({
28    firstName,
29    lastName,
30  }: {
31    firstName: string;
32    lastName: string;
33  }): string => `Hello ${firstName} ${lastName}`;
34  console.log(hello({ firstName: 'Karl', lastName: 'Marks' }));
35
36  const person = { firstName: 'Adam', lastName: 'Smith' };
37  console.log(hello(person));
38}
39
40function destructuringAssignments(): void {
41  let a = 5,
42    b = 10,
43    c = 'value';
44  ({ b, c } = { b: 200, c: 'bar' });
45  [a, b] = [b, a];
46
47  const rest: number[];
48  [a, b, ...rest] = [10, 20, 30, 40, 50];
49
50  let list = [1, 2];
51  list = [...list, 3, 4];
52
53  const e: number;
54  let f: number;
55  const x: { e: number };
56  ({
57    a,
58    b: {
59      c,
60      d: [e, f],
61    },
62  } = { a: 10, b: { c: 'foo', d: [30, 40] } });
63  [a, b, [x, { f }]] = [1, 2, [{ e: 20 }, { f: 5 }]];
64}
65
66function destructuringDeclarations(): void {
67  const { q, w, e } = { q: 1, w: 'foo', e: true };
68
69  function getSomeObject() {
70    return { x: 1, y: 2 };
71  }
72  const { x, y } = getSomeObject();
73
74  const [i, j, k] = [10, 20, 30];
75
76  const getArray = (): number[] => [1, 2, 3];
77  const [a, b] = getArray();
78}
79
80function loopVariables(): void {
81  const objects: { a: number; b: string }[] = [
82    { a: 10, b: 'q' },
83    { a: 20, b: 'w' },
84    { a: 30, b: 'e' },
85  ];
86  for (const { a, b } of objects) {
87    console.log(a, b);
88  }
89
90  const arrays = [
91    [1, 2],
92    [3, 4],
93    [5, 6],
94  ];
95  for (const [q, w] of arrays) {
96    console.log(q, w);
97  }
98
99  let a: number, b: string;
100  for ({ a, b } of objects) {
101    console.log(a, b);
102  }
103
104  let x: number, y: number;
105  for ([x, y] of arrays) {
106    console.log(x, y);
107  }
108
109  const people = [
110    {
111      name: 'Mike Smith',
112      family: { mother: 'Jane Smith', father: 'Harry Smith' },
113    },
114    {
115      name: 'Tom Jones',
116      family: { mother: 'Norah Jones', father: 'Richard Jones' },
117    },
118  ];
119  let n: string, f: string;
120  for ({
121    name: n,
122    family: { father: f },
123  } of people) {
124    console.log(`Name: ${n}, Father: ${f}`);
125  }
126}
127
128interface I {
129  d: number
130}
131
132function f1({d = 1}: I) {
133}
134f1({d:2})
135
136function f2({d = 1}: {d: number}) {
137}
138f2({d:2})
139