/* * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ type UT1 = "abc"|"bcd"|"cde" let a: UT1 = "bcd" function foo(a: T): string { let b: "abc"|"ff" = a assert b == "abc" b = "ff" return b } class A{} function bar(x: "abc"|"bcd"|A|int) { return x } function baz(x: "ab1"|"bcd"|A|int): "ab1"|string|A|int { return x } let map: Record<"aa" | "bb" | int, number> = { "aa": 1, "bb" : 3, 42 : 33, } function f1(x: number|string, y: boolean|"abc"): boolean { return x == y } function f2(x: number|string, y: string): boolean { return x == y } function f3(a: "aa"|"bb"|"cc" = "bb") { return a; } function f4(a: (p: string) => "aa"|"bb", b: (p: "aa"|"bb") => string) { b = a b(a("aa")) } function f5(x: "aa"|"bbb") { return x.length } function main(): void { assert foo<"abc">("abc") == "ff"; assert foo<"ff"|"abc">("abc") == "ff"; assert a == "bcd" assert bar("abc") == "abc" assert bar(42) == 42 let x = baz("bcd") x = "some string" assert x == "some string" assert baz("ab1") == "ab1" x = "abc" assert f1("abc", x) assert f2("abc", x) assert !f2("abc", "xyz") assert f3() == "bb" assert f3("aa") == "aa" assert f5("aa") == 2 assert f5("bbb") == 3 }