• 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
16const TopLevelSym: string = "TopLevelSym";
17const InnerSym: string = "InnerSym";
18
19const _topLevelFunc = (x: number | undefined): number => {
20    if (x === undefined) {
21        return 12;
22    }
23    return x;
24};
25
26const _innerFunc = (arg: { x: number } | undefined): number => {
27    if (arg === undefined) {
28        return 12;
29    }
30    return arg.x;
31};
32
33type NumberFunc = () => number;
34
35// Indexable type for items
36type IndexableType = Record<string, NumberFunc>;
37
38// Explicitly typed indexable object
39const _items: IndexableType = {};
40const innerFunction: NumberFunc = () => {
41    return _innerFunc({ x: 12 });
42};
43
44// Fix: Assign computed property safely (done after object creation)
45_items[InnerSym] = innerFunction;
46
47const topLevelWrapper: NumberFunc = () => {
48    return _topLevelFunc(12);
49};
50
51// Inner map using the same indexable type
52const innerMap: IndexableType = {};
53const innerMapFunction: NumberFunc = () => {
54    const result = _innerFunc({ x: 12 });
55    return result;
56};
57
58innerMap[InnerSym] = innerMapFunction;
59
60// Define the exported structure explicitly
61type ExportedType = Record<string, NumberFunc | IndexableType> & {
62    items: IndexableType;
63};
64
65// Create a base object without computed properties
66class ExportedStructure implements ExportedType {
67    items: IndexableType;
68
69    constructor() {
70        this.items = innerMap;
71    }
72
73    [key: string]: NumberFunc | IndexableType;
74
75    static createBase(): ExportedType {
76        return new ExportedStructure() as ExportedType;
77    }
78}
79
80// Create the exported structure and assign dynamic keys safely
81const baseExportedStructure: ExportedType = ExportedStructure.createBase();
82const _exportedStructure: ExportedType = { ...baseExportedStructure };
83
84// Assign computed key after object creation (not in literal)
85_exportedStructure[TopLevelSym] = topLevelWrapper;
86
87const _exported: ExportedType = _exportedStructure;
88
89export default _exported;
90
91/* @@? 26:26 Error SyntaxError: Using object literals to declare types in place is not supported. Please declare types and interfaces explicitly! */
92/* @@? 41:12 Error TypeError: No matching call signature for (...) */
93/* @@? 41:23 Error TypeError: need to specify target type for class composite */
94/* @@? 54:20 Error TypeError: No matching call signature for (...) */
95/* @@? 54:31 Error TypeError: need to specify target type for class composite */
96/* @@? 61:64 Error SyntaxError: Unexpected token '&'. */
97/* @@? 61:66 Error SyntaxError: Unexpected token '{'. */
98/* @@? 62:12 Error SyntaxError: Label must be followed by a loop statement. */
99/* @@? 62:12 Error TypeError: Type name 'IndexableType' used in the wrong context */
100/* @@? 66:36 Error TypeError: Interface expected here. */
101/* @@? 73:6 Error SyntaxError: Unexpected token 'key'. */
102/* @@? 73:9 Error SyntaxError: Unexpected token ':'. */
103/* @@? 73:17 Error SyntaxError: Field type annotation expected. */
104/* @@? 73:17 Error SyntaxError: Unexpected token ']'. */
105/* @@? 73:18 Error SyntaxError: Unexpected token ':'. */
106/* @@? 73:31 Error SyntaxError: Unexpected token '|'. */
107/* @@? 73:31 Error SyntaxError: Field type annotation expected. */
108/* @@? 73:46 Error SyntaxError: Field type annotation expected. */
109/* @@? 76:16 Error TypeError: Cannot cast type 'ExportedStructure' to 'Record<String,() => Double|Record<String,() => Double>>' */
110/* @@? 82:44 Error SyntaxError: Property or signature expected. */
111