• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
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    assertEQ(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
50// function f3(a: "aa"|"bb"|"cc" = "bb") { // #22952
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    assertEQ(foo<"abc">("abc"), "ff")
66    assertEQ(foo<"ff"|"abc">("abc"), "ff")
67
68    assertEQ(a, "bcd")
69
70    assertEQ(bar("abc"), "abc")
71    assertEQ(bar(42), 42)
72
73    let x = baz("bcd")
74    x = "some string"
75    assertEQ(x, "some string")
76    assertEQ(baz("ab1"), "ab1")
77    x = "abc"
78
79    assertTrue(f1("abc", x))
80    assertTrue(f2("abc", x))
81    assertTrue(!f2("abc", "xyz"))
82
83    // #22952
84    // assertEQ(f3(), "bb")
85    // assertEQ(f3("aa"), "aa")
86
87    assertEQ(f5("aa"), 2)
88    assertEQ(f5("bbb"), 3)
89}
90