• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16class A {
17    fld1: Int = 42
18    fld2: double = 42.0
19    fld3: [string, number] = ["abc", 42.0]
20    fld4: [string, string] = ["def", "42.0"]
21    fld5: string | number = 42.0
22    fld6: string | number = 42.0
23    fld7: string | number | null = 42.0
24    fld8: Map<string, string> = new Map<string, string>()
25}
26
27class B {
28    fld1: Number = 33.0
29    fld2: Double = 33.0
30    fld3: string[] = ["ghi", "jkl"]
31    fld4: string[] = ["mno", "pqr"]
32    fld5: string | number | null = 33.0
33    fld6: string | number | undefined = 33.0
34    fld7: string | number | undefined = 33.0
35    fld8: Map<string, number> = new Map<string, number>()
36}
37
38function getUnion(): A | B {
39    return new A
40}
41
42function main() {
43    const u = getUnion()
44    assertEQ(/* @@ label1 */u.fld1, 42)
45    assertEQ(/* @@ label2 */u.fld2, 42.0)
46    assertEQ(/* @@ label3 */u.fld3[0], "abc")
47    assertEQ(/* @@ label4 */u.fld4[0], "def")
48    assertEQ(/* @@ label5 */u.fld5, 42.0)
49    assertEQ(/* @@ label6 */u.fld6, 42.0)
50    assertEQ(/* @@ label7 */u.fld7, 42.0)
51    assertEQ(/* @@ label8 */u.fld8, new Map<string, string>())
52}
53
54/* @@@ label1 Error TypeError: Member type must be the same for all union objects.  */
55/* @@@ label2 Error TypeError: Member type must be the same for all union objects.  */
56/* @@@ label3 Error TypeError: Member type must be the same for all union objects.  */
57/* @@@ label4 Error TypeError: Member type must be the same for all union objects.  */
58/* @@@ label5 Error TypeError: Member type must be the same for all union objects.  */
59/* @@@ label6 Error TypeError: Member type must be the same for all union objects.  */
60/* @@@ label7 Error TypeError: Member type must be the same for all union objects.  */
61/* @@@ label8 Error TypeError: Member type must be the same for all union objects.  */
62