1/* 2 * Copyright (c) 2021-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 A<T extends Object> { 17 union0: T; 18} 19class B { 20 num: int = 42 21} 22class C { 23 num: int = 43 24} 25 26function foo(x: B|C) { 27 if (x instanceof B) { 28 assert x.num == 42: "Error! num field must be 42"; 29 } else if (x instanceof C) { 30 assert x.num == 43: "Error! num field must be 43"; 31 } else { 32 assert false: "Error! x must be instanceof B|C"; 33 } 34} 35 36function bar(x: A<B|C>) { 37 if (x.union0 instanceof B) { 38 assert x.union0.num == 42: "Error! x.union0.num field must be 42"; 39 } else if (x.union0 instanceof C) { 40 assert x.union0.num == 43: "Error! x.union0.num field must be 43"; 41 } else { 42 assert false: "Error! x.union0 must be instanceof B|C"; 43 } 44} 45 46function main(): void { 47 let a: A<B|C> = new A<B|C>(); 48 a.union0 = new B(); 49 foo(a.union0); 50 bar(a); 51 a.union0 = new C(); 52 bar(a); 53 foo(a.union0); 54} 55