1/* 2 * Copyright (c) 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 16type UT1 = "abc"|"bcd"|"cde" 17let a: UT1 = "bcd" 18 19function foo<T extends "abc"|"ff">(a: T): string { 20 let b: "abc"|"ff" = a 21 assert b == "abc" 22 b = "ff" 23 return b 24} 25 26class A{} 27 28function bar(x: "abc"|"bcd"|A|int) { 29 return x 30} 31 32function baz(x: "ab1"|"bcd"|A|int): "ab1"|string|A|int { 33 return x 34} 35 36let map: Record<"aa" | "bb" | int, number> = { 37 "aa": 1, 38 "bb" : 3, 39 42 : 33, 40} 41 42function f1(x: number|string, y: boolean|"abc"): boolean { 43 return x == y 44} 45 46function f2(x: number|string, y: string): boolean { 47 return x == y 48} 49 50function f3(a: "aa"|"bb"|"cc" = "bb") { 51 return a; 52} 53 54function f4(a: (p: string) => "aa"|"bb", 55 b: (p: "aa"|"bb") => string) { 56 b = a 57 b(a("aa")) 58} 59 60function f5(x: "aa"|"bbb") { 61 return x.length 62} 63 64function main(): void { 65 assert foo<"abc">("abc") == "ff"; 66 assert foo<"ff"|"abc">("abc") == "ff"; 67 68 assert a == "bcd" 69 70 assert bar("abc") == "abc" 71 assert bar(42) == 42 72 73 let x = baz("bcd") 74 x = "some string" 75 assert x == "some string" 76 assert baz("ab1") == "ab1" 77 x = "abc" 78 79 assert f1("abc", x) 80 assert f2("abc", x) 81 assert !f2("abc", "xyz") 82 83 assert f3() == "bb" 84 assert f3("aa") == "aa" 85 86 assert f5("aa") == 2 87 assert f5("bbb") == 3 88} 89