• 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
16// Negative test cases for readonly union types - these should fail compilation
17
18// Error: readonly on a non-applicable primitive type in a union
19function test_negative_1(param: readonly number | string): void {
20}
21
22// Error: readonly on a non-applicable object type in a union
23class MyObject {}
24function test_negative_2(param: readonly MyObject | string): void {
25}
26
27// Error: readonly on a non-applicable type, even if another member is applicable
28function test_negative_5(param: readonly boolean | string[]): void {
29}
30
31// Error: readonly on void type
32function test_negative_6(param: readonly void | number): void {
33}
34
35// Error: readonly on function type
36function test_negative_7(param: readonly (() => void) | string): void {
37}
38
39function main(): void {
40    // Function calls to trigger parsing of parameter types
41    test_negative_1(42, "test");
42    test_negative_2(new MyObject(), "test");
43    test_negative_5(true, ["test"]);
44    test_negative_6(undefined, 42);
45    test_negative_7(() => {}, "test");
46}
47
48
49/* @@? 19:49 Error SyntaxError: 'readonly' type modifier is only permitted on resizable array and tuple types. */
50/* @@? 24:51 Error SyntaxError: 'readonly' type modifier is only permitted on resizable array and tuple types. */
51/* @@? 28:50 Error SyntaxError: 'readonly' type modifier is only permitted on resizable array and tuple types. */
52/* @@? 32:33 Error TypeError: 'void' used as type annotation. */
53/* @@? 32:47 Error SyntaxError: 'readonly' type modifier is only permitted on resizable array and tuple types. */
54/* @@? 36:55 Error SyntaxError: 'readonly' type modifier is only permitted on resizable array and tuple types. */
55/* @@? 41:5 Error TypeError: No matching call signature for test_negative_1(int, "test") */
56/* @@? 41:5 Error TypeError: Expected 1 arguments, got 2. */
57/* @@? 42:5 Error TypeError: Expected 1 arguments, got 2. */
58/* @@? 42:5 Error TypeError: No matching call signature for test_negative_2(MyObject, "test") */
59/* @@? 43:5 Error TypeError: Expected 1 arguments, got 2. */
60/* @@? 43:5 Error TypeError: No matching call signature for test_negative_5(boolean, Array<String>) */
61/* @@? 44:5 Error TypeError: Expected 1 arguments, got 2. */
62/* @@? 44:5 Error TypeError: No matching call signature for test_negative_6(undefined, int) */
63/* @@? 45:5 Error TypeError: Expected 1 arguments, got 2. */
64/* @@? 45:5 Error TypeError: No matching call signature for test_negative_7(() => void, "test") */
65