1/* 2 * Copyright (c) 2024-2025 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 A { 17 readonly indices: number[][] | null; 18 constructor() { 19 this.indices = new number[2][2]; 20 } 21 22 public equals(other: Object | null | undefined): boolean { 23 for (let i = 0; i < this.indices!.length; ++i) { 24 let num: number[] = this.indices![i]; 25 num[0]; 26 } 27 return false; 28 } 29} 30 31function foo1(value: number, opt?: string) { 32 return "" + value + opt; 33} 34function foo2(value: number, opt: string | undefined = undefined) { 35 return "" + value + opt; 36} 37function testfoo() { 38 assertEQ(foo1(12 as number), "12undefined") // inference blocked 39 assertEQ(foo1(12, "s"), "12s") 40 assertEQ(foo1(12, undefined), "12undefined") 41 42 assertEQ(foo2(12 as number), "12undefined") // inference blocked 43 assertEQ(foo2(12, "s"), "12s") 44 assertEQ(foo2(12, undefined), "12undefined") 45} 46 47class DNode<T> { 48 readonly parent: DNode<T> | undefined = undefined; 49} 50 51class DTree<T> { 52 private current: DNode<T> = new DNode<T>(); 53 up() { 54 let parent = this.current.parent 55 this.current = parent! 56 } 57} 58 59class B { 60 public b: boolean = true; 61}; 62 63 64function dummy(): B | undefined { 65 return new B(); 66} 67 68function testb(): void { 69 let b: B | undefined = dummy(); 70 if (b?.b == false) { 71 b!.b = true; 72 } 73} 74 75class Test { 76 n: number | undefined = undefined; 77} 78 79type int32 = int; 80 81function dummy1(): int32 | null { 82 return null; 83} 84 85function testf(a: int32 | undefined): int32 { 86 let x: int32 | null = dummy1(); 87 return a! + x!; 88} 89 90function testcond() { 91 let _a: null | undefined = null, options_x: null | undefined = null; 92 return (_a = options_x) !== null && _a != undefined ? _a : 1; 93} 94 95type float32 = float; 96function foo(param?: float32) { } 97 98export class Matrix33 { 99 constructor() { this.arr = new Float32Array(8) } 100 constructor(arr: Float32Array) { this.arr = arr } 101 private arr: Float32Array | null = null; 102 103 rotate(degrees: float32, pivotX?: float32, pivotY?: float32): Matrix33 { 104 let rads = degrees * Math.PI / 180 105 let cos = Math.cos(rads) 106 let sin = Math.sin(rads) 107 let newarr = new Float32Array(8) 108 newarr.set([cos, -sin, 0, cos, 0, 0, 0, 1]); 109 return new Matrix33(newarr); 110 } 111} 112 113function main() { 114 new A().equals(null); 115 testfoo(); 116 try { 117 new DTree<string | number>().up(); 118 assertTrue(false) 119 } catch (e: NullPointerError) { } 120 testb(); 121 try { 122 testf(123); 123 assertTrue(false) 124 } catch (e: NullPointerError) { } 125 assertTrue((testcond() instanceof Int) && (testcond() == 1)) 126 127 foo(123); 128 foo(undefined); 129 new Matrix33().rotate(1, 2, 3) 130} 131