• 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
16function getTypeInfo(param: readonly number[] | string): string {
17    if (typeof param === "string") {
18        return "string";
19    } else {
20        return "array";
21    }
22}
23
24function getTypeInfoGeneric(param: readonly Array<number> | boolean): string {
25    if (typeof param === "boolean") {
26        return "boolean";
27    } else {
28        return "generic_array";
29    }
30}
31
32function getMultiUnionType(param: readonly string[] | number | boolean): string {
33    if (typeof param === "boolean") {
34        return "boolean";
35    } else if (typeof param === "number") {
36        return "number";
37    } else {
38        return "readonly_string_array";
39    }
40}
41
42function getDoubleReadonlyArrays(param: readonly number[] | readonly string[]): string {
43    // Simple type check without complex operations
44    return "readonly_array";
45}
46
47function getTupleUnion(param: readonly [number, string] | boolean): string {
48    if (typeof param === "boolean") {
49        return "boolean";
50    } else {
51        return "readonly_tuple";
52    }
53}
54
55function getReadonlyWithNull(param: readonly string[] | null): string {
56    if (param === null) {
57        return "null";
58    } else {
59        return "readonly_string_array";
60    }
61}
62
63function main(): void {
64    // Test basic readonly union - the core fix
65    let result1 = getTypeInfo("hello");
66    arktest.assertEQ(result1, "string");
67
68    let result2 = getTypeInfo([1, 2, 3] as readonly number[]);
69    arktest.assertEQ(result2, "array");
70
71    // Test readonly generic union
72    let result3 = getTypeInfoGeneric(true);
73    arktest.assertEQ(result3, "boolean");
74
75    let result4 = getTypeInfoGeneric([10, 20] as readonly Array<number>);
76    arktest.assertEQ(result4, "generic_array");
77
78    let result5 = getMultiUnionType(true);
79    arktest.assertEQ(result5, "boolean");
80
81    let result6 = getMultiUnionType(42);
82    arktest.assertEQ(result6, "number");
83
84    let result7 = getMultiUnionType(["a", "b"] as readonly string[]);
85    arktest.assertEQ(result7, "readonly_string_array");
86
87    // Test readonly number[] | readonly string[]
88    let result8 = getDoubleReadonlyArrays([1, 2] as readonly number[]);
89    arktest.assertEQ(result8, "readonly_array");
90
91    let result9 = getDoubleReadonlyArrays(["x", "y"] as readonly string[]);
92    arktest.assertEQ(result9, "readonly_array");
93
94    // Test readonly tuple
95    let result10 = getTupleUnion(true);
96    arktest.assertEQ(result10, "boolean");
97
98    let result11 = getTupleUnion([42, "test"] as readonly [number, string]);
99    arktest.assertEQ(result11, "readonly_tuple");
100
101    // Test readonly with null
102    let result12 = getReadonlyWithNull(null);
103    arktest.assertEQ(result12, "null");
104
105    let result13 = getReadonlyWithNull(["test"] as readonly string[]);
106    arktest.assertEQ(result13, "readonly_string_array");
107}