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,156 @@
1
+ # 对象可能为 null 错误
2
+
3
+ ## 错误描述
4
+
5
+ 在 ArkTS 中,编译器会严格检查可能为 null 的对象访问。如果对象可能为 null,但直接访问其属性,会导致"Object is possibly 'null'"错误。
6
+
7
+ ### 错误信息
8
+
9
+ ```
10
+ Object is possibly 'null'
11
+ ```
12
+
13
+
14
+
15
+ ```
16
+ Variable 'xxx' is possibly null
17
+ ```
18
+
19
+ ## 错误示例
20
+
21
+ ```typescript
22
+ // ❌ 错误:对象可能为 null
23
+ let display = display.getDefaultDisplaySync();
24
+ console.log(display.width); // 错误:Object is possibly 'null'
25
+
26
+ // ❌ 错误:条件判断不完整
27
+ if (display) {
28
+ console.log(display.width); // 某些情况下仍可能为 null
29
+ }
30
+ ```
31
+
32
+ ## 解决方案
33
+
34
+ ### 方案一:使用 !== null 检查
35
+
36
+ 显式检查对象不等于 null:
37
+
38
+ ```typescript
39
+ // ✅ 正确:使用 !== null 检查
40
+ let display = display.getDefaultDisplaySync();
41
+ if (display !== null) {
42
+ console.log(display.width);
43
+ }
44
+ ```
45
+
46
+ ### 方案二:使用可选链和空值合并
47
+
48
+ ```typescript
49
+ // ✅ 正确:使用可选链和空值合并
50
+ let display = display.getDefaultDisplaySync();
51
+ let width = display?.width ?? 0;
52
+ ```
53
+
54
+ ### 方案三:使用 let 声明可空类型
55
+
56
+ ```typescript
57
+ // ✅ 正确:显式声明可空类型
58
+ let display: Display | null = display.getDefaultDisplaySync();
59
+ if (display !== null) {
60
+ console.log(display.width);
61
+ }
62
+ ```
63
+
64
+ ### 方案四:非空断言(谨慎使用)
65
+
66
+ ```typescript
67
+ // ✅ 正确(但需确保不会为 null):使用非空断言
68
+ let display = display.getDefaultDisplaySync()!;
69
+ console.log(display.width);
70
+ ```
71
+
72
+ ## 简单示例
73
+
74
+ ```typescript
75
+ import { display } from '@kit.ArkUI';
76
+
77
+ @Entry
78
+ @Component
79
+ struct NullCheckExample {
80
+ private myDisplay: Display | null = null;
81
+
82
+ aboutToAppear() {
83
+ this.myDisplay = display.getDefaultDisplaySync();
84
+ }
85
+
86
+ build() {
87
+ Column() {
88
+ if (this.myDisplay !== null) {
89
+ Text(`Width: ${this.myDisplay.width}`)
90
+ .fontSize(24)
91
+ }
92
+ }
93
+ .width('100%')
94
+ }
95
+ }
96
+ ```
97
+
98
+ ## 详细代码示例
99
+
100
+ - [PossiblyNullError.ets](../assets/PossiblyNullError.ets) - 完整的 null 检查错误修复示例
101
+
102
+ ## 最佳实践
103
+
104
+ 1. **优先使用 !== null 检查**:最安全的方式,显式检查对象不为 null
105
+ 2. **使用可选链**:`?.` 可以在对象为 null 时返回 undefined
106
+ 3. **使用空值合并**:`??` 提供默认值
107
+ 4. **避免非空断言**:除非确定对象不会为 null,否则不要使用 `!`
108
+ 5. **类型注解**:对可能为 null 的变量显式声明联合类型
109
+
110
+ ## 常见场景
111
+
112
+ ### Display API
113
+
114
+ ```typescript
115
+ // ❌ 错误
116
+ let display = display.getDefaultDisplaySync();
117
+ let width = display.width;
118
+
119
+ // ✅ 正确
120
+ let display = display.getDefaultDisplaySync();
121
+ if (display !== null) {
122
+ let width = display.width;
123
+ }
124
+ ```
125
+
126
+ ### Window API
127
+
128
+ ```typescript
129
+ // ❌ 错误
130
+ let window = window.getLastWindow(context);
131
+ window.setFullScreen(true);
132
+
133
+ // ✅ 正确
134
+ let window = await window.getLastWindow(context);
135
+ if (window !== null) {
136
+ await window.setFullScreen(true);
137
+ }
138
+ ```
139
+
140
+ ### 可选属性
141
+
142
+ ```typescript
143
+ // ❌ 错误
144
+ let config = { width: 100 };
145
+ let w = config.height; // height 不存在
146
+
147
+ // ✅ 正确
148
+ interface Config {
149
+ width: number;
150
+ height?: number;
151
+ }
152
+ let config: Config = { width: 100 };
153
+ if (config.height !== undefined) {
154
+ let h = config.height;
155
+ }
156
+ ```
@@ -0,0 +1,154 @@
1
+ # Resource 类型转换错误
2
+
3
+ ## 错误描述
4
+
5
+ 在 ArkTS 中,不能将 `Resource` 类型直接转换为 `string` 或 `number` 类型。Resource 是一个特殊的资源引用类型,需要通过特定的方式使用。
6
+
7
+ ## 错误示例
8
+
9
+ ```typescript
10
+ const message: string = $r('app.string.hello');
11
+ const fontSize: number = $r('app.float.title_font_size');
12
+ ```
13
+
14
+ **错误信息:**
15
+ ```
16
+ Conversion of type 'Resource' to type 'string'/'number' may be a mistake because neither type sufficiently overlaps with the other.
17
+ ```
18
+
19
+ ## 解决方案
20
+
21
+ ### 方案1:直接在 UI 组件中使用(推荐)
22
+
23
+ Resource 类型可以直接作为属性值传递给 UI 组件,系统会自动处理资源解析。
24
+
25
+ ```typescript
26
+ @Entry
27
+ @Component
28
+ struct MyComponent {
29
+ build() {
30
+ Column() {
31
+ Text($r('app.string.hello'))
32
+ .fontSize($r('app.float.title_font_size'))
33
+ .width($r('app.float.layout_width'))
34
+ .height($r('app.float.layout_height'))
35
+ .backgroundColor($r('app.color.background_color'))
36
+ }
37
+ }
38
+ }
39
+ ```
40
+
41
+ ### 方案2:使用资源管理器获取字符串值
42
+
43
+ 如果需要获取字符串的实际值,可以使用 `ResourceManager`。
44
+
45
+ ```typescript
46
+ import { resourceManager } from '@kit.LocalizationKit';
47
+
48
+ async function getStringResource(context: Context, resourceId: number): Promise<string> {
49
+ const manager = context.resourceManager;
50
+ return await manager.getString(resourceId);
51
+ }
52
+ ```
53
+
54
+ ### 方案3:使用 getNumber 获取数值资源
55
+
56
+ 对于数值资源,使用 `getNumber` 方法。
57
+
58
+ ```typescript
59
+ import { resourceManager } from '@kit.LocalizationKit';
60
+
61
+ async function getNumberResource(context: Context, resourceId: number): Promise<number> {
62
+ const manager = context.resourceManager;
63
+ return await manager.getNumber(resourceId);
64
+ }
65
+ ```
66
+
67
+ ### 方案4:使用 getStringByName 根据名称获取
68
+
69
+ 根据资源名称获取字符串值。
70
+
71
+ ```typescript
72
+ import { resourceManager } from '@kit.LocalizationKit';
73
+
74
+ async function getStringByName(context: Context, name: string): Promise<string> {
75
+ const manager = context.resourceManager;
76
+ return await manager.getStringByName(name);
77
+ }
78
+ ```
79
+
80
+ ## 详细说明
81
+
82
+ Resource 类型的特点:
83
+
84
+ 1. **延迟加载**:资源在需要时才被解析
85
+ 2. **多语言支持**:根据系统语言自动选择对应的资源
86
+ 3. **主题适配**:支持深色/浅色主题切换
87
+ 4. **类型安全**:编译时检查资源引用的正确性
88
+
89
+ ## 资源类型对照表
90
+
91
+ | 资源类型 | $r 语法 | 资源文件位置 | 示例 |
92
+ |---------|---------|-------------|------|
93
+ | 字符串 | `$r('app.string.name')` | `resources/base/element/string.json` | `$r('app.string.hello')` |
94
+ | 颜色 | `$r('app.color.name')` | `resources/base/element/color.json` | `$r('app.color.primary')` |
95
+ | 浮点数 | `$r('app.float.name')` | `resources/base/element/float.json` | `$r('app.float.title_font_size')` |
96
+ | 整数 | `$r('app.integer.name')` | `resources/base/element/integer.json` | `$r('app.integer.max_count')` |
97
+ | 布尔 | `$r('app.boolean.name')` | `resources/base/element/boolean.json` | `$r('app.boolean.is_enabled')` |
98
+ | 媒体 | `$r('app.media.name')` | `resources/base/media/` | `$r('app.media.icon')` |
99
+
100
+ ## 简单示例
101
+
102
+ ```typescript
103
+ @Entry
104
+ @Component
105
+ struct ResourceUsageExample {
106
+ @State displayText: string = '';
107
+
108
+ async loadStringResource() {
109
+ try {
110
+ const context = this.getUIContext().getHostContext();
111
+ const manager = context.resourceManager;
112
+ this.displayText = await manager.getString($r('app.string.hello').id);
113
+ } catch (err) {
114
+ console.error(`Failed to load string resource: ${JSON.stringify(err)}`);
115
+ }
116
+ }
117
+
118
+ build() {
119
+ Column() {
120
+ Text('Resource 类型使用示例')
121
+ .fontSize($r('app.float.title_font_size'))
122
+ .fontWeight(FontWeight.Bold)
123
+ .margin(20)
124
+
125
+ Text($r('app.string.hello'))
126
+ .fontSize($r('app.float.content_font_size'))
127
+ .width($r('app.float.layout_width'))
128
+ .padding($r('app.float.padding'))
129
+ .backgroundColor($r('app.color.background_color'))
130
+ .borderRadius($r('app.float.border_radius'))
131
+ .margin(20)
132
+
133
+ Button('加载字符串资源')
134
+ .onClick(() => {
135
+ this.loadStringResource();
136
+ })
137
+ .margin(20)
138
+
139
+ if (this.displayText) {
140
+ Text(`加载的字符串: ${this.displayText}`)
141
+ .fontSize(14)
142
+ .margin(20)
143
+ }
144
+ }
145
+ .width('100%')
146
+ .height('100%')
147
+ .padding(20)
148
+ }
149
+ }
150
+ ```
151
+
152
+ ## 详细代码示例
153
+
154
+ > [ResourceConversionError.ets](../assets/ResourceConversionError.ets) - 完整的 Resource 类型转换错误示例和修复方案
@@ -0,0 +1,68 @@
1
+ # Standalone Function `this` Usage Error
2
+
3
+ ## 错误描述
4
+
5
+ 在独立函数(非类方法)中不能直接使用 `this`,因为 `this` 在独立函数中没有上下文绑定。
6
+
7
+ ## 错误示例
8
+
9
+ ```typescript
10
+ async function getAvoidArea() {
11
+ const win = await window.getLastWindow(this.getUIContext().getHostContext());
12
+ return win.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
13
+ }
14
+ ```
15
+
16
+ **错误信息:**
17
+ ```
18
+ Cannot find name 'this'
19
+ ```
20
+
21
+ ## 解决方案
22
+
23
+ 将上下文作为参数传递给独立函数。
24
+
25
+ ```typescript
26
+ async function getAvoidArea(context: common.UIAbilityContext): Promise<window.AvoidArea> {
27
+ return new Promise((resolve, reject) => {
28
+ window.getLastWindow(context, (err, win) => {
29
+ if (err.code !== 0) {
30
+ reject(err);
31
+ return;
32
+ }
33
+ const avoidArea = win.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
34
+ resolve(avoidArea);
35
+ });
36
+ });
37
+ }
38
+ ```
39
+
40
+ ## 简单示例
41
+
42
+ ```typescript
43
+ @Entry
44
+ @Component
45
+ struct Example {
46
+ @State avoidAreaHeight: number = 0;
47
+
48
+ async aboutToAppear() {
49
+ const context = this.getUIContext().getHostContext() as common.UIAbilityContext;
50
+ try {
51
+ const avoidArea = await getAvoidArea(context);
52
+ this.avoidAreaHeight = avoidArea.topRect.height;
53
+ } catch (err) {
54
+ console.error('获取避让区域失败:', err);
55
+ }
56
+ }
57
+
58
+ build() {
59
+ Column() {
60
+ Text(`避让区域高度: ${this.avoidAreaHeight}`)
61
+ }
62
+ }
63
+ }
64
+ ```
65
+
66
+ ## 详细代码示例
67
+
68
+ - [StandaloneFunctionError.ets](../assets/StandaloneFunctionError.ets) - 完整的独立函数上下文传递示例,包含多个独立函数的使用
@@ -0,0 +1,124 @@
1
+ # @StorageLink Default Value Errors
2
+
3
+ ## Error: `The '@StorageLink' property must be specified a default value`
4
+
5
+ ### Error Message
6
+ ```
7
+ The '@StorageLink' property must be specified a default value
8
+ ```
9
+
10
+ ### Cause
11
+ ArkTS requires all `@StorageLink` decorated properties to have a default value. This is a strict type safety requirement to ensure the property always has a valid initial state.
12
+
13
+ ### Solution
14
+ Add a default value to the `@StorageLink` property, typically `= undefined` for optional types or a specific default value for required types.
15
+
16
+ ### Key Points
17
+ - Always provide a default value for `@StorageLink` properties
18
+ - Use `= undefined` for optional types
19
+ - Use specific default values for required types (e.g., `= 0`, `= ''`, `= false`)
20
+ - Initialize the actual value in `aboutToAppear()` using `AppStorage.setOrCreate()`
21
+
22
+ ### Basic Pattern
23
+ ```typescript
24
+ @Entry
25
+ @Component
26
+ struct MyComponent {
27
+ @StorageLink('myKey') myValue: number = 0;
28
+
29
+ aboutToAppear() {
30
+ AppStorage.setOrCreate('myKey', 0);
31
+ }
32
+
33
+ build() {
34
+ Text(`Value: ${this.myValue}`)
35
+ }
36
+ }
37
+ ```
38
+
39
+ ### Optional Type Pattern
40
+ ```typescript
41
+ interface WindowUtil {
42
+ width: number;
43
+ height: number;
44
+ density: number;
45
+ updateWindowInfo: () => void;
46
+ destroy: () => void;
47
+ }
48
+
49
+ @Entry
50
+ @Component
51
+ struct MyComponent {
52
+ @StorageLink('windowUtil') windowUtil?: WindowUtil = undefined;
53
+
54
+ aboutToAppear() {
55
+ AppStorage.setOrCreate('windowUtil', {
56
+ width: 0,
57
+ height: 0,
58
+ density: 1.0,
59
+ updateWindowInfo: () => {},
60
+ destroy: () => {}
61
+ });
62
+ }
63
+
64
+ build() {
65
+ Text(`Width: ${this.windowUtil?.width || 0}`)
66
+ }
67
+ }
68
+ ```
69
+
70
+ ### Common Default Values
71
+ ```typescript
72
+ // Number types
73
+ @StorageLink('counter') count: number = 0;
74
+ @StorageLink('width') width: number = 100;
75
+ @StorageLink('opacity') opacity: number = 1.0;
76
+
77
+ // String types
78
+ @StorageLink('userName') userName: string = '';
79
+ @StorageLink('title') title: string = 'Default Title';
80
+
81
+ // Boolean types
82
+ @StorageLink('isDarkMode') isDarkMode: boolean = false;
83
+ @StorageLink('isLoading') isLoading: boolean = true;
84
+
85
+ // Array types
86
+ @StorageLink('items') items: Array<string> = [];
87
+ @StorageLink('numbers') numbers: number[] = [1, 2, 3];
88
+
89
+ // Optional types
90
+ @StorageLink('user') user?: User = undefined;
91
+ @StorageLink('settings') settings?: Settings = undefined;
92
+ ```
93
+
94
+ ### Detailed Examples
95
+ For more detailed code examples, see:
96
+ - [Optional Type Pattern](../assets/StorageLinkDefaultError.ets#L8-L31)
97
+ - [Initialization Pattern](../assets/StorageLinkDefaultError.ets#L11-L23)
98
+ - [Usage Pattern](../assets/StorageLinkDefaultError.ets#L25-L39)
99
+
100
+ ### Best Practices
101
+ 1. **Always provide default value**: Never leave `@StorageLink` without a default
102
+ 2. **Use appropriate defaults**: Choose defaults that make sense for your use case
103
+ 3. **Initialize in aboutToAppear**: Set the actual value when component appears
104
+ 4. **Use optional types carefully**: Only use `?` when the value can legitimately be undefined
105
+ 5. **Document default values**: Add comments explaining why a specific default was chosen
106
+
107
+ ### Common Mistakes
108
+ ```typescript
109
+ // ❌ Wrong: No default value
110
+ @StorageLink('myValue') myValue: number;
111
+
112
+ // ❌ Wrong: Using null instead of undefined
113
+ @StorageLink('myValue') myValue: number = null;
114
+
115
+ // ✅ Correct: With default value
116
+ @StorageLink('myValue') myValue: number = 0;
117
+
118
+ // ✅ Correct: Optional type with undefined
119
+ @StorageLink('myValue') myValue?: number = undefined;
120
+ ```
121
+
122
+ ### Related Files
123
+ - [Code Example](../assets/StorageLinkDefaultError.ets)
124
+ - [AppStorage Type Errors](./appstorage_errors.md)
@@ -0,0 +1,167 @@
1
+ # TitleButtonRect Type Error
2
+
3
+ ## 错误描述
4
+
5
+ `window.getTitleButtonRect()` 方法返回的是 `window.TitleButtonRect` 类型,而不是 `window.Rect` 类型。如果函数返回类型声明为 `window.Rect`,会导致类型不匹配错误。
6
+
7
+ ## 错误信息
8
+
9
+ ### 错误 1: 类型不匹配
10
+ ```
11
+ Argument of type 'TitleButtonRect' is not assignable to parameter of type 'Rect | PromiseLike<Rect>'.
12
+ Property 'left' is missing in type 'TitleButtonRect' but required in type 'Rect'.
13
+ ```
14
+
15
+ ### 错误 2: 访问不存在的属性
16
+ ```
17
+ Property 'left' does not exist on type 'TitleButtonRect'.
18
+ Property 'top' does not exist on type 'TitleButtonRect'.
19
+ ```
20
+
21
+ **原因**:`TitleButtonRect` 类型只包含 `width` 和 `height` 属性,不包含 `left` 和 `top` 属性。
22
+
23
+ ## 错误示例
24
+
25
+ ```typescript
26
+ async function getTitleButtonRect(context: common.UIAbilityContext): Promise<window.Rect> {
27
+ return new Promise((resolve, reject) => {
28
+ window.getLastWindow(context, (err, win) => {
29
+ if (err.code !== 0) {
30
+ reject(new Error(err.message));
31
+ return;
32
+ }
33
+ const titleButtonRect = win.getTitleButtonRect();
34
+ resolve(titleButtonRect);
35
+ });
36
+ });
37
+ }
38
+ ```
39
+
40
+ ## 解决方案
41
+
42
+ 将函数返回类型从 `window.Rect` 改为 `window.TitleButtonRect`。
43
+
44
+ ```typescript
45
+ async function getTitleButtonRect(context: common.UIAbilityContext): Promise<window.TitleButtonRect> {
46
+ return new Promise((resolve, reject) => {
47
+ window.getLastWindow(context, (err, win) => {
48
+ if (err.code !== 0) {
49
+ reject(new Error(err.message));
50
+ return;
51
+ }
52
+ const titleButtonRect = win.getTitleButtonRect();
53
+ resolve(titleButtonRect);
54
+ });
55
+ });
56
+ }
57
+ ```
58
+
59
+ ## 类型说明
60
+
61
+ `window.TitleButtonRect` 和 `window.Rect` 是两个不同的类型:
62
+
63
+ - `window.Rect`: 包含 `left`, `top`, `width`, `height` 属性
64
+ - `window.TitleButtonRect`: **只包含** `width`, `height` 属性(不包含 `left` 和 `top` 属性)
65
+
66
+ **重要提示**:`TitleButtonRect` 类型只提供宽度和高度信息,不包含位置信息(left 和 top)。如果需要位置信息,需要使用其他 API 获取。
67
+
68
+ ## 简单示例
69
+
70
+ ```typescript
71
+ import { window } from '@kit.ArkUI';
72
+ import { common } from '@kit.AbilityKit';
73
+
74
+ async function getTitleButtonRect(context: common.UIAbilityContext): Promise<window.TitleButtonRect> {
75
+ return new Promise((resolve, reject) => {
76
+ window.getLastWindow(context, (err, win) => {
77
+ if (err.code !== 0) {
78
+ reject(new Error(err.message));
79
+ return;
80
+ }
81
+ const titleButtonRect = win.getTitleButtonRect();
82
+ resolve(titleButtonRect);
83
+ });
84
+ });
85
+ }
86
+
87
+ @Entry
88
+ @Component
89
+ struct TitleButtonRectExample {
90
+ @State titleBarHeight: number = 0;
91
+ @State titleBarWidth: number = 0;
92
+
93
+ async aboutToAppear() {
94
+ const context = this.getUIContext().getHostContext() as common.UIAbilityContext;
95
+ try {
96
+ const titleButtonRect = await getTitleButtonRect(context);
97
+ // TitleButtonRect 只包含 width 和 height 属性
98
+ this.titleBarHeight = titleButtonRect.height;
99
+ this.titleBarWidth = titleButtonRect.width;
100
+ // ❌ 错误:不能访问 left 和 top 属性
101
+ // this.titleBarLeft = titleButtonRect.left; // Property 'left' does not exist
102
+ // this.titleBarTop = titleButtonRect.top; // Property 'top' does not exist
103
+ } catch (err) {
104
+ console.error('获取标题栏按钮区域失败:', err instanceof Error ? err.message : String(err));
105
+ }
106
+ }
107
+
108
+ build() {
109
+ Column() {
110
+ Text(`标题栏高度: ${this.titleBarHeight}`)
111
+ Text(`标题栏宽度: ${this.titleBarWidth}`)
112
+ }
113
+ }
114
+ }
115
+ ```
116
+
117
+ ## 详细代码示例
118
+
119
+ - [TitleButtonRectTypeError.ets](../assets/TitleButtonRectTypeError.ets) - TitleButtonRect 类型错误的完整示例,包含错误和正确的解决方案
120
+ - [StandaloneFunctionContext.ets](../assets/StandaloneFunctionContext.ets#L34-L47) - 完整的 TitleButtonRect 类型使用示例,包含错误处理
121
+ - [StandaloneFunctionError.ets](../assets/StandaloneFunctionError.ets#L34-L47) - 独立函数中的 TitleButtonRect 类型使用示例
122
+
123
+ ## 相关类型
124
+
125
+ | 类型 | 说明 | 用途 |
126
+ |------|------|------|
127
+ | `window.Rect` | 通用矩形区域 | 窗口区域、避让区域等 |
128
+ | `window.TitleButtonRect` | 标题栏按钮区域 | 标题栏按钮的位置和大小 |
129
+ | `window.AvoidArea` | 避让区域 | 系统栏、导航栏等避让区域 |
130
+
131
+ ## 最佳实践
132
+
133
+ 1. **使用正确的返回类型**: 根据实际调用的 API 返回类型来声明函数返回类型
134
+ 2. **查看 API 文档**: 使用窗口 API 时,仔细查看返回值类型和可用属性
135
+ 3. **了解类型差异**: `TitleButtonRect` 只包含 `width` 和 `height`,不包含位置信息
136
+ 4. **类型转换**: 如果需要在不同类型之间转换,创建新的对象而不是直接赋值
137
+ 5. **避免访问不存在的属性**: 不要尝试访问 `TitleButtonRect` 的 `left` 和 `top` 属性
138
+ 6. **使用 TypeScript 类型推断**: 在某些情况下,可以省略返回类型注解让编译器推断
139
+
140
+ ## 常见错误
141
+
142
+ ```typescript
143
+ // ❌ 错误:返回类型声明为 Rect
144
+ async function getTitleButtonRect(): Promise<window.Rect> {
145
+ const win = await window.getLastWindow(context);
146
+ return win.getTitleButtonRect();
147
+ }
148
+
149
+ // ✅ 正确:返回类型声明为 TitleButtonRect
150
+ async function getTitleButtonRect(): Promise<window.TitleButtonRect> {
151
+ const win = await window.getLastWindow(context);
152
+ return win.getTitleButtonRect();
153
+ }
154
+
155
+ // ✅ 正确:如果需要 Rect 类型,进行类型转换
156
+ // 注意:TitleButtonRect 不包含 left 和 top 属性,只能提供 width 和 height
157
+ async function getTitleButtonRectAsRect(): Promise<window.Rect> {
158
+ const win = await window.getLastWindow(context);
159
+ const titleButtonRect = win.getTitleButtonRect();
160
+ return {
161
+ left: 0, // TitleButtonRect 不提供位置信息,需要从其他 API 获取
162
+ top: 0, // TitleButtonRect 不提供位置信息,需要从其他 API 获取
163
+ width: titleButtonRect.width,
164
+ height: titleButtonRect.height
165
+ };
166
+ }
167
+ ```