deveco-harness 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (135) hide show
  1. package/README.en.md +71 -0
  2. package/README.md +71 -0
  3. package/deveco-harness.example.jsonc +32 -0
  4. package/dist/index.js +758 -0
  5. package/package.json +48 -0
  6. package/prompts/build.txt +164 -0
  7. package/prompts/plan.txt +118 -0
  8. package/skills/arkts-error-fixes/README.md +151 -0
  9. package/skills/arkts-error-fixes/SKILL.md +150 -0
  10. package/skills/arkts-error-fixes/assets/AnyTypeError.ets +269 -0
  11. package/skills/arkts-error-fixes/assets/AppStorageError.ets +22 -0
  12. package/skills/arkts-error-fixes/assets/ArrowFunctionConversionError.ets +42 -0
  13. package/skills/arkts-error-fixes/assets/AvoidAreaTypeError.ets +43 -0
  14. package/skills/arkts-error-fixes/assets/BreakpointTypeError.ets +71 -0
  15. package/skills/arkts-error-fixes/assets/CatchClauseTypeError.ets +54 -0
  16. package/skills/arkts-error-fixes/assets/ColorConsistencyError.ets +88 -0
  17. package/skills/arkts-error-fixes/assets/ColorPropertyError.ets +39 -0
  18. package/skills/arkts-error-fixes/assets/ContextTypeError.ets +105 -0
  19. package/skills/arkts-error-fixes/assets/DecoratorStateError.ets +64 -0
  20. package/skills/arkts-error-fixes/assets/DisplayListenerTypeError.ets +129 -0
  21. package/skills/arkts-error-fixes/assets/DuplicateEntryError.ets +35 -0
  22. package/skills/arkts-error-fixes/assets/ESObjectTypeError.ets +137 -0
  23. package/skills/arkts-error-fixes/assets/FontColorPropertyError.ets +42 -0
  24. package/skills/arkts-error-fixes/assets/FunctionReturnTypeError.ets +41 -0
  25. package/skills/arkts-error-fixes/assets/IDataSourceError.ets +79 -0
  26. package/skills/arkts-error-fixes/assets/ImplementationNotAllowedError.ets +38 -0
  27. package/skills/arkts-error-fixes/assets/InterfaceMethodSignatureError.ets +43 -0
  28. package/skills/arkts-error-fixes/assets/NotificationError.ets +34 -0
  29. package/skills/arkts-error-fixes/assets/ObjectLiteralInterfaceError.ets +49 -0
  30. package/skills/arkts-error-fixes/assets/ObjectLiteralTypeError.ets +54 -0
  31. package/skills/arkts-error-fixes/assets/ObjectSpreadError.ets +29 -0
  32. package/skills/arkts-error-fixes/assets/PossiblyNullError.ets +32 -0
  33. package/skills/arkts-error-fixes/assets/ResourceConversionError.ets +159 -0
  34. package/skills/arkts-error-fixes/assets/StandaloneFunctionContext.ets +116 -0
  35. package/skills/arkts-error-fixes/assets/StandaloneFunctionError.ets +85 -0
  36. package/skills/arkts-error-fixes/assets/StorageLinkDefaultError.ets +41 -0
  37. package/skills/arkts-error-fixes/assets/TitleButtonRectTypeError.ets +86 -0
  38. package/skills/arkts-error-fixes/assets/UnusedVariableWarning.ets +102 -0
  39. package/skills/arkts-error-fixes/assets/UtilityTypeError.ets +176 -0
  40. package/skills/arkts-error-fixes/assets/WindowRectSizeError.ets +65 -0
  41. package/skills/arkts-error-fixes/assets/WindowTypeError.ets +37 -0
  42. package/skills/arkts-error-fixes/reference/any_type_errors.md +433 -0
  43. package/skills/arkts-error-fixes/reference/appstorage_errors.md +95 -0
  44. package/skills/arkts-error-fixes/reference/arrow_function_conversion_errors.md +163 -0
  45. package/skills/arkts-error-fixes/reference/avoid_area_type_errors.md +59 -0
  46. package/skills/arkts-error-fixes/reference/breakpoint_type_errors.md +122 -0
  47. package/skills/arkts-error-fixes/reference/catch_clause_type_errors.md +89 -0
  48. package/skills/arkts-error-fixes/reference/color_consistency_errors.md +209 -0
  49. package/skills/arkts-error-fixes/reference/color_property_errors.md +136 -0
  50. package/skills/arkts-error-fixes/reference/context_type_errors.md +260 -0
  51. package/skills/arkts-error-fixes/reference/decorator_state_errors.md +109 -0
  52. package/skills/arkts-error-fixes/reference/display_listener_type_errors.md +149 -0
  53. package/skills/arkts-error-fixes/reference/duplicate_entry_errors.md +193 -0
  54. package/skills/arkts-error-fixes/reference/esobject_type_errors.md +110 -0
  55. package/skills/arkts-error-fixes/reference/fontcolor_property_errors.md +104 -0
  56. package/skills/arkts-error-fixes/reference/function_return_type_errors.md +194 -0
  57. package/skills/arkts-error-fixes/reference/idata_source_errors.md +77 -0
  58. package/skills/arkts-error-fixes/reference/implementation_not_allowed_errors.md +191 -0
  59. package/skills/arkts-error-fixes/reference/interface_method_signature_errors.md +143 -0
  60. package/skills/arkts-error-fixes/reference/notification_errors.md +110 -0
  61. package/skills/arkts-error-fixes/reference/object_literal_interface_errors.md +156 -0
  62. package/skills/arkts-error-fixes/reference/object_literal_type_errors.md +110 -0
  63. package/skills/arkts-error-fixes/reference/object_spread_errors.md +105 -0
  64. package/skills/arkts-error-fixes/reference/possibly_null_errors.md +156 -0
  65. package/skills/arkts-error-fixes/reference/resource_conversion_errors.md +154 -0
  66. package/skills/arkts-error-fixes/reference/standalone_function_errors.md +68 -0
  67. package/skills/arkts-error-fixes/reference/storage_link_default_errors.md +124 -0
  68. package/skills/arkts-error-fixes/reference/title_button_rect_type_errors.md +167 -0
  69. package/skills/arkts-error-fixes/reference/unused_variable_warnings.md +295 -0
  70. package/skills/arkts-error-fixes/reference/utility_type_errors.md +290 -0
  71. package/skills/arkts-error-fixes/reference/window_rect_size_errors.md +70 -0
  72. package/skills/arkts-error-fixes/reference/window_type_errors.md +95 -0
  73. package/skills/arkts-grammar-standards/SKILL.md +71 -0
  74. package/skills/arkts-grammar-standards/references/basic-syntax.md +103 -0
  75. package/skills/arkts-grammar-standards/references/restrictions.md +100 -0
  76. package/skills/arkts-grammar-standards/references/topic-aliases.json +93 -0
  77. package/skills/arkts-grammar-standards/references/ts-diff.md +108 -0
  78. package/skills/arkts-runtime-fix/SKILL.md +152 -0
  79. package/skills/arkts-runtime-fix/SKILL_CN.md +123 -0
  80. package/skills/arkts-runtime-fix/evals/evals.json +17 -0
  81. package/skills/arkts-runtime-fix/scripts/collect-hilog.mjs +116 -0
  82. package/skills/arkts-runtime-fix/scripts/collect-hilog.ts +115 -0
  83. package/skills/arkts-runtime-fix/scripts/fetch-faultlog.mjs +117 -0
  84. package/skills/arkts-runtime-fix/scripts/fetch-faultlog.ts +116 -0
  85. package/skills/arkts-runtime-fix/scripts/jscrash-report.mjs +149 -0
  86. package/skills/arkts-runtime-fix/scripts/parse-jscrash-log.mjs +126 -0
  87. package/skills/arkts-runtime-fix/scripts/parse-jscrash-log.ts +126 -0
  88. package/skills/arkts-runtime-fix/scripts/probe-faultlogger.mjs +160 -0
  89. package/skills/arkts-runtime-fix/scripts/probe-faultlogger.ts +160 -0
  90. package/skills/arkts-runtime-fix/scripts/shared/hdc.mjs +107 -0
  91. package/skills/arkts-runtime-fix/scripts/shared/hdc.ts +58 -0
  92. package/skills/arkts-runtime-fix/scripts/shared/jscrash-faultlogger.mjs +143 -0
  93. package/skills/arkts-runtime-fix/scripts/shared/jscrash-faultlogger.ts +148 -0
  94. package/skills/arkts-runtime-fix/scripts/shared/jscrash-parse.mjs +389 -0
  95. package/skills/arkts-runtime-fix/scripts/shared/jscrash-parse.ts +409 -0
  96. package/skills/arkts-runtime-fix/scripts/shared/utils.mjs +27 -0
  97. package/skills/arkts-runtime-fix/scripts/shared/utils.ts +27 -0
  98. package/skills/arkui-knowledge/SKILL.md +90 -0
  99. package/skills/arkui-knowledge/references/api-guardrails.md +60 -0
  100. package/skills/arkui-knowledge/references/common-mistakes.md +119 -0
  101. package/skills/arkui-knowledge/references/component-cookbook.md +135 -0
  102. package/skills/arkui-knowledge/references/ui-quality-checklist.md +36 -0
  103. package/skills/deveco-create-project/SKILL.md +144 -0
  104. package/skills/deveco-create-project/application/AppScope/app.json5 +10 -0
  105. package/skills/deveco-create-project/application/AppScope/resources/base/element/string.json +8 -0
  106. package/skills/deveco-create-project/application/AppScope/resources/base/media/background.png +0 -0
  107. package/skills/deveco-create-project/application/AppScope/resources/base/media/foreground.png +0 -0
  108. package/skills/deveco-create-project/application/AppScope/resources/base/media/layered_image.json +7 -0
  109. package/skills/deveco-create-project/application/build-profile.json5 +42 -0
  110. package/skills/deveco-create-project/application/code-linter.json5 +32 -0
  111. package/skills/deveco-create-project/application/entry/build-profile.json5 +33 -0
  112. package/skills/deveco-create-project/application/entry/hvigorfile.ts +7 -0
  113. package/skills/deveco-create-project/application/entry/obfuscation-rules.txt +23 -0
  114. package/skills/deveco-create-project/application/entry/oh-package.json5 +10 -0
  115. package/skills/deveco-create-project/application/entry/src/main/ets/entryability/EntryAbility.ets +63 -0
  116. package/skills/deveco-create-project/application/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets +31 -0
  117. package/skills/deveco-create-project/application/entry/src/main/ets/pages/Index.ets +38 -0
  118. package/skills/deveco-create-project/application/entry/src/main/module.json5 +50 -0
  119. package/skills/deveco-create-project/application/entry/src/main/resources/base/element/color.json +8 -0
  120. package/skills/deveco-create-project/application/entry/src/main/resources/base/element/float.json +8 -0
  121. package/skills/deveco-create-project/application/entry/src/main/resources/base/element/string.json +16 -0
  122. package/skills/deveco-create-project/application/entry/src/main/resources/base/media/background.png +0 -0
  123. package/skills/deveco-create-project/application/entry/src/main/resources/base/media/foreground.png +0 -0
  124. package/skills/deveco-create-project/application/entry/src/main/resources/base/media/layered_image.json +7 -0
  125. package/skills/deveco-create-project/application/entry/src/main/resources/base/media/startIcon.png +0 -0
  126. package/skills/deveco-create-project/application/entry/src/main/resources/base/profile/backup_config.json +3 -0
  127. package/skills/deveco-create-project/application/entry/src/main/resources/base/profile/main_pages.json +5 -0
  128. package/skills/deveco-create-project/application/entry/src/main/resources/dark/element/color.json +8 -0
  129. package/skills/deveco-create-project/application/hvigor/hvigor-config.json5 +23 -0
  130. package/skills/deveco-create-project/application/hvigorfile.ts +7 -0
  131. package/skills/deveco-create-project/application/oh-package.json5 +10 -0
  132. package/skills/deveco-create-project/scripts/copy-template.mjs +216 -0
  133. package/skills/deveco-create-project/scripts/copy-template.ts +214 -0
  134. package/skills/deveco-create-project/scripts/detect-sdk.mjs +82 -0
  135. package/skills/deveco-create-project/scripts/detect-sdk.ts +95 -0
@@ -0,0 +1,110 @@
1
+ # ESObject 类型限制错误
2
+
3
+ ## 错误描述
4
+
5
+ 在 ArkTS 中,`ESObject` 类型的使用受到限制,不能直接用作变量类型声明。这是为了确保类型安全和避免运行时错误。
6
+
7
+ ## 错误示例
8
+
9
+ ```typescript
10
+ let nfcModule: ESObject = null;
11
+ ```
12
+
13
+ **错误信息:**
14
+ ```
15
+ Usage of "ESObject" type is restricted (arkts-limited-esobj)
16
+ ```
17
+
18
+ ## 解决方案
19
+
20
+ ### 方案1:使用动态导入(推荐)
21
+
22
+ 对于需要动态导入的模块,使用 `import()` 函数并使用 `Promise` 或 `ESModule` 类型。
23
+
24
+ ```typescript
25
+ let nfcModule: ESModule | null = null;
26
+ try {
27
+ nfcModule = import('@kit.ConnectivityKit');
28
+ } catch (err) {
29
+ console.error(`Failed to import NFC module: ${JSON.stringify(err)}`);
30
+ }
31
+ ```
32
+
33
+ ### 方案2:使用具体类型
34
+
35
+ 如果知道模块的具体类型,使用具体的接口或类。
36
+
37
+ ```typescript
38
+ import { nfcController } from '@kit.ConnectivityKit';
39
+
40
+ let nfcControllerInstance: nfcController.NfcController | null = null;
41
+ ```
42
+
43
+ ### 方案3:使用 unknown 类型
44
+
45
+ 如果不确定模块类型,可以使用 `unknown` 类型,但需要在使用时进行类型检查。
46
+
47
+ ```typescript
48
+ let module: unknown = null;
49
+ try {
50
+ module = import('@kit.ConnectivityKit');
51
+ if (module !== null && module !== undefined) {
52
+ // 使用前进行类型检查
53
+ console.info('Module imported successfully');
54
+ }
55
+ } catch (err) {
56
+ console.error(`Failed to import module: ${JSON.stringify(err)}`);
57
+ }
58
+ ```
59
+
60
+ ## 详细说明
61
+
62
+ ArkTS 限制 `ESObject` 类型的使用是为了:
63
+
64
+ 1. **类型安全**:避免运行时类型错误
65
+ 2. **编译时检查**:确保类型使用的正确性
66
+ 3. **模块系统规范**:遵循 ES 模块标准
67
+
68
+ ## 简单示例
69
+
70
+ ```typescript
71
+ @Entry
72
+ @Component
73
+ struct DynamicImportExample {
74
+ @State moduleLoaded: boolean = false;
75
+ @State moduleName: string = '';
76
+
77
+ async loadNFCModule() {
78
+ try {
79
+ const nfcModule = await import('@kit.ConnectivityKit');
80
+ this.moduleLoaded = true;
81
+ this.moduleName = 'NFC';
82
+ console.info('NFC module imported successfully');
83
+ } catch (err) {
84
+ console.error(`Failed to import NFC module: ${JSON.stringify(err)}`);
85
+ this.moduleLoaded = false;
86
+ this.moduleName = 'NFC';
87
+ }
88
+ }
89
+
90
+ build() {
91
+ Column() {
92
+ Button('Load NFC Module')
93
+ .onClick(() => {
94
+ this.loadNFCModule();
95
+ })
96
+ .margin(20)
97
+
98
+ if (this.moduleLoaded) {
99
+ Text(`${this.moduleName} module loaded successfully`)
100
+ .fontSize(16)
101
+ .margin(20)
102
+ }
103
+ }
104
+ }
105
+ }
106
+ ```
107
+
108
+ ## 详细代码示例
109
+
110
+ > [ESObjectTypeError.ets](../assets/ESObjectTypeError.ets) - 完整的ESObject类型错误示例和修复方案
@@ -0,0 +1,104 @@
1
+ # fontColor 属性错误
2
+
3
+ ## 错误描述
4
+
5
+ `fontColor` 属性只能用于文本组件(如 `Text`、`Span`、`Button` 等),不能用于容器组件(如 `Column`、`Row`、`Stack` 等)。在容器组件上使用 `fontColor` 会导致编译错误。
6
+
7
+ ## 错误示例
8
+
9
+ ```typescript
10
+ @Entry
11
+ @Component
12
+ struct FontColorError {
13
+ build() {
14
+ Column() {
15
+ Text('Hello World')
16
+ }
17
+ .fontColor('#FF0000')
18
+ }
19
+ }
20
+ ```
21
+
22
+ **错误信息:**
23
+ ```
24
+ Property 'fontColor' does not exist on type 'ColumnAttribute'
25
+ ```
26
+
27
+ ## 解决方案
28
+
29
+ ### 方案一:将 fontColor 应用到 Text 组件
30
+
31
+ 将 `fontColor` 属性从容器组件移除,直接应用到文本组件上。
32
+
33
+ ```typescript
34
+ @Entry
35
+ @Component
36
+ struct FontColorCorrect {
37
+ build() {
38
+ Column() {
39
+ Text('Hello World')
40
+ .fontColor('#FF0000')
41
+ }
42
+ }
43
+ }
44
+ ```
45
+
46
+ ## 简单示例
47
+
48
+ ```typescript
49
+ @Entry
50
+ @Component
51
+ struct FontColorExample {
52
+ build() {
53
+ Column() {
54
+ Text('标题')
55
+ .fontSize(20)
56
+ .fontWeight(FontWeight.Bold)
57
+ .fontColor('#333333')
58
+
59
+ Text('内容')
60
+ .fontSize(16)
61
+ .fontColor('#666666')
62
+ }
63
+ .width('100%')
64
+ .padding(16)
65
+ }
66
+ }
67
+ ```
68
+
69
+ ## 详细代码示例
70
+
71
+ - [FontColorPropertyError.ets](../assets/FontColorPropertyError.ets) - 完整的 fontColor 属性错误修复示例,包含正确和错误的用法对比
72
+
73
+ ## 最佳实践
74
+
75
+ 1. **仅在文本组件上使用 fontColor**:`fontColor` 属性应该只用于 `Text`、`Span`、`Button` 等文本组件
76
+ 2. **使用十六进制颜色值**:推荐使用十六进制颜色值(如 `#FF0000`)而不是 `Color` 枚举
77
+ 3. **定义颜色常量**:对于重复使用的颜色,建议定义常量
78
+ 4. **使用资源引用**:对于主题相关的颜色,使用资源引用以便于主题切换
79
+
80
+ ## 常见错误
81
+
82
+ ```typescript
83
+ // ❌ 错误:在 Column 上使用 fontColor
84
+ Column() {
85
+ Text('Hello')
86
+ }
87
+ .fontColor('#FF0000')
88
+
89
+ // ❌ 错误:在 Row 上使用 fontColor
90
+ Row() {
91
+ Text('Hello')
92
+ }
93
+ .fontColor('#FF0000')
94
+
95
+ // ✅ 正确:在 Text 上使用 fontColor
96
+ Column() {
97
+ Text('Hello')
98
+ .fontColor('#FF0000')
99
+ }
100
+
101
+ // ✅ 正确:在 Button 上使用 fontColor
102
+ Button('Click Me')
103
+ .fontColor('#FFFFFF')
104
+ ```
@@ -0,0 +1,194 @@
1
+ # Function Return Type Errors
2
+
3
+ ## Error: `Object literals cannot be used as type declarations`
4
+
5
+ ### Error Message
6
+ ```
7
+ Object literals cannot be used as type declarations (arkts-no-obj-literals-as-types)
8
+ ```
9
+
10
+ ### Cause
11
+ ArkTS does not allow object literal types (like `{ width: number; height: number }`) to be used directly as return type annotations. You must define an interface or type alias first.
12
+
13
+ ### Solution
14
+ Define an interface for the object shape, then use that interface as the return type.
15
+
16
+ ### Basic Pattern
17
+ ```typescript
18
+ // ❌ Wrong: Object literal type in return annotation
19
+ private getWindowInfo(): { width: number; height: number } {
20
+ return {
21
+ width: 1080,
22
+ height: 2340
23
+ };
24
+ }
25
+
26
+ // ✅ Correct: Define interface first
27
+ interface WindowInfo {
28
+ width: number;
29
+ height: number;
30
+ }
31
+
32
+ private getWindowInfo(): WindowInfo {
33
+ return {
34
+ width: 1080,
35
+ height: 2340
36
+ };
37
+ }
38
+ ```
39
+
40
+ ### Simple Example
41
+ ```typescript
42
+ interface WindowInfo {
43
+ width: number;
44
+ height: number;
45
+ }
46
+
47
+ @Entry
48
+ @Component
49
+ struct WindowInfoExample {
50
+ @State windowWidth: number = 0;
51
+ @State windowHeight: number = 0;
52
+
53
+ private getWindowInfo(): WindowInfo {
54
+ return {
55
+ width: 1080,
56
+ height: 2340
57
+ };
58
+ }
59
+
60
+ build() {
61
+ Column() {
62
+ Text(`Window: ${this.windowWidth} x ${this.windowHeight}`)
63
+ }
64
+ }
65
+ }
66
+ ```
67
+
68
+ ### Detailed Code Examples
69
+ - [FunctionReturnTypeError.ets](../assets/FunctionReturnTypeError.ets) - 完整的函数返回类型示例,包含接口定义和使用
70
+
71
+ ---
72
+
73
+ ## Error: `Function return type inference is limited`
74
+
75
+ ### Error Message
76
+ ```
77
+ Function return type inference is limited
78
+ ```
79
+
80
+ ### Cause
81
+ ArkTS has limited type inference for function return types, especially for complex types or when the return type cannot be easily inferred from the function body. This is a strict type safety requirement to ensure code clarity and prevent type errors.
82
+
83
+ ### Solution
84
+ Add explicit return type annotations to functions that return complex types or when the compiler cannot infer the return type.
85
+
86
+ ### Key Points
87
+ - Add explicit return type annotations for complex return types
88
+ - Use interface or type aliases for object return types
89
+ - Annotate functions that return arrays, objects, or unions
90
+ - Keep return type annotations simple and clear
91
+
92
+ ### Basic Pattern
93
+ ```typescript
94
+ // ❌ Wrong: No explicit return type
95
+ private getWindowInfo() {
96
+ return {
97
+ width: 1080,
98
+ height: 2340
99
+ };
100
+ }
101
+
102
+ // ✅ Correct: With explicit return type
103
+ private getWindowInfo(): { width: number; height: number } {
104
+ return {
105
+ width: 1080,
106
+ height: 2340
107
+ };
108
+ }
109
+ ```
110
+
111
+ ### Common Patterns
112
+ ```typescript
113
+ // Object return types
114
+ private getConfig(): Config {
115
+ return { width: 100, height: 100, color: 'red' };
116
+ }
117
+
118
+ // Array return types
119
+ interface Item {
120
+ id: number;
121
+ name: string;
122
+ }
123
+
124
+ private getItems(): Item[] {
125
+ return [
126
+ { id: 1, name: 'Item 1' },
127
+ { id: 2, name: 'Item 2' }
128
+ ];
129
+ }
130
+
131
+ // Union return types
132
+ private getValue(): string | number {
133
+ if (Math.random() > 0.5) {
134
+ return 'string';
135
+ }
136
+ return 42;
137
+ }
138
+
139
+ // Optional return types
140
+ private findUser(id: number): User | undefined {
141
+ return this.users.find(user => user.id === id);
142
+ }
143
+
144
+ // Promise return types
145
+ private async fetchData(): Promise<Data> {
146
+ const response = await fetch('https://api.example.com/data');
147
+ return await response.json();
148
+ }
149
+ ```
150
+
151
+ ### Detailed Examples
152
+ For more detailed code examples, see:
153
+ - [Object Return Type](../assets/FunctionReturnTypeError.ets#L14-L19)
154
+ - [Void Return Type](../assets/FunctionReturnTypeError.ets#L21-L26)
155
+ - [Usage Pattern](../assets/FunctionReturnTypeError.ets#L28-L35)
156
+
157
+ ### Best Practices
158
+ 1. **Add explicit types**: Always add return type annotations for complex functions
159
+ 2. **Use interfaces**: Define interfaces for object return types
160
+ 3. **Keep types simple**: Avoid overly complex return type expressions
161
+ 4. **Be consistent**: Use the same style throughout your codebase
162
+ 5. **Document types**: Add comments explaining complex return types
163
+
164
+ ### Common Mistakes
165
+ ```typescript
166
+ // ❌ Wrong: No explicit return type
167
+ private getConfig() {
168
+ return { width: 100, height: 100 };
169
+ }
170
+
171
+ // ❌ Wrong: Using 'any' type
172
+ private getConfig(): any {
173
+ return { width: 100, height: 100 };
174
+ }
175
+
176
+ // ✅ Correct: With explicit return type
177
+ private getConfig(): { width: number; height: number } {
178
+ return { width: 100, height: 100 };
179
+ }
180
+
181
+ // ✅ Correct: Using interface
182
+ interface Config {
183
+ width: number;
184
+ height: number;
185
+ }
186
+
187
+ private getConfig(): Config {
188
+ return { width: 100, height: 100 };
189
+ }
190
+ ```
191
+
192
+ ### Related Files
193
+ - [Code Example](../assets/FunctionReturnTypeError.ets)
194
+ - [ArkTS Language Guide](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-get-started)
@@ -0,0 +1,77 @@
1
+ # IDataSource 类型错误
2
+
3
+ ## 错误信息
4
+
5
+ ```
6
+ Argument of type 'string[]' is not assignable to parameter of type 'IDataSource'.
7
+ Type 'string[]' is missing the following properties from type 'IDataSource':
8
+ totalCount, getData, registerDataChangeListener, unregisterDataChangeListener
9
+ ```
10
+
11
+ ## 错误原因
12
+
13
+ 在 ArkTS 中,`LazyForEach` 要求数据源必须实现 `IDataSource` 接口,直接使用 `string[]` 会导致类型错误。
14
+
15
+ ## 错误示例
16
+
17
+ ```typescript
18
+ // 错误 - 直接使用 string[]
19
+ private data: string[] = ['Item 1', 'Item 2', 'Item 3'];
20
+
21
+ build() {
22
+ List() {
23
+ LazyForEach(this.data, (item: string) => {
24
+ ListItem() {
25
+ Text(item)
26
+ }
27
+ }, (item: string) => item)
28
+ }
29
+ }
30
+ ```
31
+
32
+ ## 解决方案
33
+
34
+ 实现 `IDataSource` 接口:
35
+
36
+ ```typescript
37
+ class MyDataSource {
38
+ data: string[] = [];
39
+ private listeners: DataChangeListener[] = [];
40
+
41
+ totalCount(): number {
42
+ return this.data.length;
43
+ }
44
+
45
+ getData(index: number): string {
46
+ return this.data[index];
47
+ }
48
+
49
+ registerDataChangeListener(listener: DataChangeListener): void {
50
+ this.listeners.push(listener);
51
+ }
52
+
53
+ unregisterDataChangeListener(listener: DataChangeListener): void {
54
+ const index = this.listeners.indexOf(listener);
55
+ if (index > -1) {
56
+ this.listeners.splice(index, 1);
57
+ }
58
+ }
59
+
60
+ pushData(data: string): void {
61
+ this.data.push(data);
62
+ this.listeners.forEach((listener: DataChangeListener) => {
63
+ listener.onDataAdd(this.data.length - 1);
64
+ });
65
+ }
66
+ }
67
+ ```
68
+
69
+ ## 详细代码示例
70
+
71
+ 请参考 [IDataSourceError.ets](../assets/IDataSourceError.ets)
72
+
73
+ ## 相关 API
74
+
75
+ - [LazyForEach](https://developer.harmonyos.com/cn/docs/documentation/doc-guides/arkts-rendering-control-000000177275贼299)
76
+ - [IDataSource](https://developer.harmonyos.com/cn/docs/documentation/doc-references/arkts-common-0000001774129201)
77
+ - [DataChangeListener](https://developer.harmonyos.com/cn/docs/documentation/doc-references/arkts-common-0000001774129201)
@@ -0,0 +1,191 @@
1
+ # 实现不允许错误
2
+
3
+ ## 错误描述
4
+
5
+ 在 ArkTS 中,UI 组件必须使用 `@Component` 装饰器装饰,并且必须在 `build()` 方法中返回 UI 结构。直接在文件中编写 UI 组件而不使用 `@Component` 装饰器会导致"实现不允许"错误。
6
+
7
+ ## 错误示例
8
+
9
+ ```typescript
10
+ // 错误:直接在文件中编写 UI 组件
11
+ Row() {
12
+ Text('Hello')
13
+ }
14
+ ```
15
+
16
+ **错误信息:**
17
+ ```
18
+ Implementation not allowed
19
+ ```
20
+
21
+ ## 解决方案
22
+
23
+ ### 方案一:使用 @Component 装饰器
24
+
25
+ 将 UI 组件包装在 `@Component` 装饰器中,并添加 `build()` 方法。
26
+
27
+ ```typescript
28
+ @Component
29
+ struct MyComponent {
30
+ build() {
31
+ Row() {
32
+ Text('Hello')
33
+ }
34
+ }
35
+ }
36
+ ```
37
+
38
+ ### 方案二:使用 @Entry 装饰器(如果是页面入口)
39
+
40
+ 如果是页面入口组件,使用 `@Entry` 装饰器。
41
+
42
+ ```typescript
43
+ @Entry
44
+ @Component
45
+ struct MyPage {
46
+ build() {
47
+ Row() {
48
+ Text('Hello')
49
+ }
50
+ }
51
+ }
52
+ ```
53
+
54
+ ## 简单示例
55
+
56
+ ```typescript
57
+ @Entry
58
+ @Component
59
+ struct MyPage {
60
+ @State count: number = 0;
61
+
62
+ build() {
63
+ Column() {
64
+ Text(`计数: ${this.count}`)
65
+ .fontSize(20)
66
+ .margin({ bottom: 16 })
67
+
68
+ Button('增加')
69
+ .onClick(() => {
70
+ this.count++;
71
+ })
72
+ }
73
+ .padding(16)
74
+ }
75
+ }
76
+ ```
77
+
78
+ ## 详细代码示例
79
+
80
+ - [ImplementationNotAllowedError.ets](../assets/ImplementationNotAllowedError.ets) - 完整的实现不允许错误修复示例,包含正确的组件结构
81
+
82
+ ## 最佳实践
83
+
84
+ 1. **使用 @Component 装饰器**:所有自定义组件都必须使用 `@Component` 装饰器
85
+ 2. **使用 @Entry 装饰器**:页面入口组件必须使用 `@Entry` 装饰器
86
+ 3. **实现 build() 方法**:所有组件都必须实现 `build()` 方法并返回 UI 结构
87
+ 4. **遵循组件结构**:UI 组件必须遵循 ArkTS 的组件结构规范
88
+
89
+ ## 常见错误
90
+
91
+ ```typescript
92
+ // ❌ 错误:直接在文件中编写 UI 组件
93
+ Row() {
94
+ Text('Hello')
95
+ }
96
+
97
+ // ❌ 错误:缺少 @Component 装饰器
98
+ struct MyComponent {
99
+ build() {
100
+ Row() {
101
+ Text('Hello')
102
+ }
103
+ }
104
+ }
105
+
106
+ // ❌ 错误:缺少 build() 方法
107
+ @Component
108
+ struct MyComponent {
109
+ // 缺少 build() 方法
110
+ }
111
+
112
+ // ✅ 正确:使用 @Component 装饰器和 build() 方法
113
+ @Component
114
+ struct MyComponent {
115
+ build() {
116
+ Row() {
117
+ Text('Hello')
118
+ }
119
+ }
120
+ }
121
+
122
+ // ✅ 正确:页面入口使用 @Entry 装饰器
123
+ @Entry
124
+ @Component
125
+ struct MyPage {
126
+ build() {
127
+ Row() {
128
+ Text('Hello')
129
+ }
130
+ }
131
+ }
132
+ ```
133
+
134
+ ## 组件结构规范
135
+
136
+ ### 基本组件结构
137
+
138
+ ```typescript
139
+ @Component
140
+ struct MyComponent {
141
+ // 状态变量
142
+ @State count: number = 0;
143
+
144
+ // 私有属性
145
+ private scroller: Scroller = new Scroller();
146
+
147
+ // 生命周期方法
148
+ aboutToAppear() {
149
+ // 组件即将出现时调用
150
+ }
151
+
152
+ aboutToDisappear() {
153
+ // 组件即将消失时调用
154
+ }
155
+
156
+ // 自定义方法
157
+ private handleClick() {
158
+ this.count++;
159
+ }
160
+
161
+ // UI 构建方法
162
+ build() {
163
+ Column() {
164
+ Text('Hello')
165
+ }
166
+ }
167
+ }
168
+ ```
169
+
170
+ ### 页面入口组件结构
171
+
172
+ ```typescript
173
+ @Entry
174
+ @Component
175
+ struct MyPage {
176
+ // 状态变量
177
+ @State title: string = 'My Page';
178
+
179
+ // 生命周期方法
180
+ aboutToAppear() {
181
+ // 页面即将出现时调用
182
+ }
183
+
184
+ // UI 构建方法
185
+ build() {
186
+ Column() {
187
+ Text(this.title)
188
+ }
189
+ }
190
+ }
191
+ ```