1/* 2 * Copyright (c) 2023-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 16class C { 17 a: any; 18 b: any[]; 19 c: [number, number]; 20 d: number[]; 21} 22 23function arrayLiteralsInVarDecl(): void { 24 const a = [1, 2]; // NOW OK 25 const b: any = [3, 4]; // NOW OK 26 const c: any[] = [5, 6]; // OK 27 const d: [number, number] = [7, 8]; // NOW OK 28 const e: number[] = [9, 10]; // OK 29 const f = [1, 'x', true]; // NOW OK 30 const g: object[] = [2, 'y', false]; // OK 31 32 const h: C = { 33 a: [1, 2], // NOW OK 34 b: [3, 4], // OK 35 c: [5, 6], // NOW OK 36 d: [7, 8], // OK 37 }; 38 39 const x = [1, 2, 3][1]; // NOW OK 40} 41 42let a: number[]; 43let b: any; 44let c: any[]; 45let d: [number, number]; 46let e: number[]; 47let f: [number, string, boolean]; 48let g: object[]; 49let h: C; 50 51function arrayLiteralsInAssignment(): void { 52 a = [1, 2]; // OK 53 b = [3, 4]; // NOW OK 54 c = [5, 6]; // OK 55 d = [7, 8]; // NOW OK 56 e = [9, 10]; // OK 57 f = [1, 'x', true]; // NOW OK for ARR.LITERAL 58 g = [2, 'y', false]; // OK 59 60 h = { 61 a: [1, 2], // NOW OK 62 b: [3, 4], // OK 63 c: [5, 6], // NOW OK 64 d: [7, 8], // OK 65 }; 66} 67 68// Default parameter value 69function foo(x = [1, 2]) { 70 return x; 71} // NOW OK 72function foo2(x: any = [3, 4]) { 73 return x; 74} // NOW OK 75function foo3(x: any[] = [5, 6]) { 76 return x; 77} // OK 78function foo4(x: [number, number] = [7, 8]) { 79 return x; 80} // NOW OK 81function foo5(x: number[] = [9, 10]) { 82 return x; 83} // OK 84 85function arrayLiteralsInFunCall(): void { 86 foo([1, 2]); // OK 87 foo2([3, 4]); // NOW OK 88 foo3([5, 6]); // OK 89 foo4([7, 8]); // NOW OK 90 foo5([9, 10]); // OK 91} 92 93// Return from function 94function bar() { 95 return [1, 2]; 96} // NOW OK 97function bar2(): any { 98 return [3, 4]; 99} // NOW OK 100function bar3(): any[] { 101 return [5, 6]; 102} // OK 103function bar4(): [number, number] { 104 return [7, 8]; 105} // NOW OK 106function bar5(): number[] { 107 return [9, 10]; 108} // OK 109 110function arrayLiteralsInTernaryOp(): void { 111 const condition = true; 112 a = condition ? [1, 2] : [3, 4]; // OK 113 b = condition ? [5, 6] : [7, 8]; // NOW OK 114 c = condition ? [9, 10] : [11, 12]; // OK 115 d = condition ? [13, 14] : [15, 16]; // NOW OK 116 e = condition ? [17, 18] : [19, 20]; // OK 117} 118 119class P { 120 n:number = 0; 121 s:string = ''; 122} 123 124let a1 = [ { n:1, s:"1" } as P, { n:2, s:"2" } as P ]; // OK 125let a2: P[] = [ { n:3, s:"3" }, { n:4, s:"4" } ]; // OK 126let a3 = [ { n:1, s:"1" }, { n:2, s:"2" } ]; // NOT OK 127