1/* 2 * Copyright (c) 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 16enum Color { 17 Red = "aaa", 18 Yellow = "bbb", 19} 20enum ColorInt { 21 A = 1111, 22 B = 2222 23} 24 25function foo(b : boolean) : int | Color | string | ColorInt { 26 if (b) { 27 return 1111; 28 } 29 return Color.Red; 30} 31 32function foo1(b : boolean) : int | Color | string | ColorInt { 33 if (b) { 34 return "bbb"; 35 } 36 return ColorInt.B; 37} 38 39function testOptional(b ?: ColorInt | int | string) : int { 40 const v1 : int = b ? b as int : 0; 41 return v1; 42} 43 44function main() { 45 const v1: Color = foo(false) as Color 46 const v2: ColorInt = foo(true) as ColorInt 47 const v3: string = foo(false) as string 48 const v4: int = foo1(false) as int 49 const v5: Color = foo1(true) as Color 50 const v6: ColorInt = foo1(false) as ColorInt 51 52 assertEQ(v1, Color.Red) 53 assertEQ(v2, ColorInt.A) 54 assertEQ(v3, "aaa") 55 assertEQ(v4, 2222) 56 assertEQ(v5, Color.Yellow) 57 assertEQ(v6, ColorInt.B) 58 59 const a : Color | string | boolean = Color.Red 60 const b : string | Color | boolean = Color.Red 61 assertEQ(a, Color.Red) 62 assertEQ(b, Color.Red) 63 64 assertEQ(testOptional(ColorInt.A), 1111) 65 assertEQ(testOptional(), 0); 66} 67