1/* 2 * Copyright (c) 2022-2024 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 16enum Mammals { 17 Humans = 'Humans', 18 Bats = 'Bats', 19 Dolphins = 'Dolphins', 20} 21 22for (const m in Mammals) { 23 if (true) console.log(m); 24} 25 26/** 27 * OK, spec 7.5.2 Type inference from Types of Elements 28 * type is (number | string | boolean)[], it is inferred from types of the elements. 29 */ 30const someArray = [1, 'string', false]; 31for (const entry of someArray) { 32 console.log(entry); // 1, "string", false 33} 34 35for (const entry of [1, 'string', false]) { 36 console.log(entry); // 1, "string", false 37} 38 39/** 40 * NOT OK 41 * type is explicitly specified to be tuple 42 */ 43const someTuple: [number, string, boolean] = [1, 'string', false]; 44for (const entry of someTuple) { 45 console.log(entry); // 1, "string", false 46} 47 48const list = [4, 5, 6]; 49for (const i in list) { 50 if (true) console.log(i); // "0", "1", "2", 51} 52 53for (const i of list) { 54 console.log(i); // 4, 5, 6 55} 56 57for (const i of [4, 5, 6]) { 58 console.log(i); // 4, 5, 6 59} 60 61const s = 'abc'; // type is 'abc' 62for (const c of s) { 63 console.log(c); 64} 65 66let s2 = 'abs' // type is string 67for (const c of s2) { 68 console.log(c); 69} 70 71for (const c of 'abc') { 72 console.log(c); 73} 74 75for (const i in s) { 76 if (true) console.log(i); 77} 78 79const arr = ['z', 'x', 'y']; 80 81for (const c of arr) { 82 console.log(c); 83} 84 85let i: number, j: number, k: number; 86// legal 'comma' operator 87for (i = 1, j = 2, k = 3; i * j * k > 0; i++, k--, j += 2) { 88 continue; 89} 90// illegal 'comma' operator 91for (i = 1, ((j = 2), (k = 3)); i * j * k > 0; i--, (k++, j--)) { 92 continue; 93} 94 95const a1 = [0, 1]; 96const a2 = [1, 2, 3, 4, 5]; 97const a3: number[] = [1, 2, 3, 4, 5]; 98const a4: Array<number> = [1, 2, 3, 4, 5]; 99const a5 = new Array(10); 100const a6 = new Int8Array(10); 101const a7 = new Uint8Array(10); 102const a8 = new Uint8ClampedArray(10); 103const a9 = new Int16Array(10); 104const a10 = new Uint16Array(10); 105const a11 = new Int32Array(10); 106const a12 = new Uint32Array(10); 107const a13 = new Float32Array(10); 108const a14 = new Float64Array(10); 109const a15 = new BigInt64Array(10); 110const a16 = new BigUint64Array(10); 111 112for (const i of a1) { 113 console.log(i); 114} 115for (const i of a2) { 116 console.log(i); 117} 118for (const i of a3) { 119 console.log(i); 120} 121for (const i of a4) { 122 console.log(i); 123} 124for (const i of a5) { 125 console.log(i); 126} 127for (const i of a6) { 128 console.log(i); 129} 130for (const i of a7) { 131 console.log(i); 132} 133for (const i of a8) { 134 console.log(i); 135} 136for (const i of a9) { 137 console.log(i); 138} 139for (const i of a10) { 140 console.log(i); 141} 142for (const i of a11) { 143 console.log(i); 144} 145for (const i of a12) { 146 console.log(i); 147} 148for (const i of a13) { 149 console.log(i); 150} 151for (const i of a14) { 152 console.log(i); 153} 154for (const i of a15) { 155 console.log(i); 156} 157for (const i of a16) { 158 console.log(i); 159} 160 161const a17: (number | string)[] = [] 162 163for (const i of a17) { 164 console.log(i) 165} 166 167class P<T> extends Array<T> { 168 foo() { 169 for (let t of this) { 170 console.log(t) 171 } 172 } 173} 174 175let a18 = new P<number>(1, 2, 3) 176a18.foo() 177for (let t of a18) { 178 console.log(t) 179} 180 181class PN extends Array<number> { 182 foo() { 183 for (let t of this) { 184 console.log(t) 185 } 186 } 187} 188 189let a19 = new PN(1, 2, 3) 190a19.foo() 191for (let t of a19) { 192 console.log(t) 193} 194 195class MyInt8Array extends Int8Array {} 196class MyUint8Array extends Uint8Array {} 197class MyUint8ClampedArray extends Uint8ClampedArray {} 198class MyInt16Array extends Int16Array {} 199class MyUint16Array extends Uint16Array {} 200class MyInt32Array extends Int32Array {} 201class MyUint32Array extends Uint32Array {} 202class MyFloat32Array extends Float32Array {} 203class MyFloat64Array extends Float64Array {} 204class MyBigInt64Array extends BigInt64Array {} 205class MyBigUint64Array extends BigUint64Array {} 206 207const a20 = new MyInt8Array(10); 208const a21 = new MyUint8Array(10); 209const a22 = new MyUint8ClampedArray(10); 210const a23 = new MyInt16Array(10); 211const a24 = new MyUint16Array(10); 212const a25 = new MyInt32Array(10); 213const a26 = new MyUint32Array(10); 214const a27 = new MyFloat32Array(10); 215const a28 = new MyFloat64Array(10); 216const a29 = new MyBigInt64Array(10); 217const a30 = new MyBigUint64Array(10); 218 219for (const i of a20) { 220 console.log(i); 221} 222for (const i of a21) { 223 console.log(i); 224} 225for (const i of a22) { 226 console.log(i); 227} 228for (const i of a23) { 229 console.log(i); 230} 231for (const i of a24) { 232 console.log(i); 233} 234for (const i of a25) { 235 console.log(i); 236} 237for (const i of a26) { 238 console.log(i); 239} 240for (const i of a27) { 241 console.log(i); 242} 243for (const i of a28) { 244 console.log(i); 245} 246for (const i of a29) { 247 console.log(i); 248} 249for (const i of a30) { 250 console.log(i); 251} 252 253class A<type1, type2> extends Map<type1, type2> {} 254class B<type> extends Set<type> {} 255class C extends String {} 256 257let map = new Map([["a", 1], ["b", 2], ["c", 3]]); 258let derivedFromMap = new A([["d", 4], ["e", 5], ["f", 6]]); 259let set = new Set<number>([1, 2, 3]); 260let derivedFromSet = new B<number>([4, 5, 6]); 261let str = "hello"; 262let derivedFromString = new C("world") 263 264for (let i of map) { 265 console.log(i); 266} 267 268for (let i of derivedFromMap) { 269 console.log(i); 270} 271 272for (let i of set) { 273 console.log(i); 274} 275 276for (let i of derivedFromSet) { 277 console.log(i); 278} 279 280for (let i of str) { 281 console.log(i); 282} 283 284for (let i of derivedFromString) { 285 console.log(i); 286} 287