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.
- package/README.en.md +71 -0
- package/README.md +71 -0
- package/deveco-harness.example.jsonc +32 -0
- package/dist/index.js +758 -0
- package/package.json +48 -0
- package/prompts/build.txt +164 -0
- package/prompts/plan.txt +118 -0
- package/skills/arkts-error-fixes/README.md +151 -0
- package/skills/arkts-error-fixes/SKILL.md +150 -0
- package/skills/arkts-error-fixes/assets/AnyTypeError.ets +269 -0
- package/skills/arkts-error-fixes/assets/AppStorageError.ets +22 -0
- package/skills/arkts-error-fixes/assets/ArrowFunctionConversionError.ets +42 -0
- package/skills/arkts-error-fixes/assets/AvoidAreaTypeError.ets +43 -0
- package/skills/arkts-error-fixes/assets/BreakpointTypeError.ets +71 -0
- package/skills/arkts-error-fixes/assets/CatchClauseTypeError.ets +54 -0
- package/skills/arkts-error-fixes/assets/ColorConsistencyError.ets +88 -0
- package/skills/arkts-error-fixes/assets/ColorPropertyError.ets +39 -0
- package/skills/arkts-error-fixes/assets/ContextTypeError.ets +105 -0
- package/skills/arkts-error-fixes/assets/DecoratorStateError.ets +64 -0
- package/skills/arkts-error-fixes/assets/DisplayListenerTypeError.ets +129 -0
- package/skills/arkts-error-fixes/assets/DuplicateEntryError.ets +35 -0
- package/skills/arkts-error-fixes/assets/ESObjectTypeError.ets +137 -0
- package/skills/arkts-error-fixes/assets/FontColorPropertyError.ets +42 -0
- package/skills/arkts-error-fixes/assets/FunctionReturnTypeError.ets +41 -0
- package/skills/arkts-error-fixes/assets/IDataSourceError.ets +79 -0
- package/skills/arkts-error-fixes/assets/ImplementationNotAllowedError.ets +38 -0
- package/skills/arkts-error-fixes/assets/InterfaceMethodSignatureError.ets +43 -0
- package/skills/arkts-error-fixes/assets/NotificationError.ets +34 -0
- package/skills/arkts-error-fixes/assets/ObjectLiteralInterfaceError.ets +49 -0
- package/skills/arkts-error-fixes/assets/ObjectLiteralTypeError.ets +54 -0
- package/skills/arkts-error-fixes/assets/ObjectSpreadError.ets +29 -0
- package/skills/arkts-error-fixes/assets/PossiblyNullError.ets +32 -0
- package/skills/arkts-error-fixes/assets/ResourceConversionError.ets +159 -0
- package/skills/arkts-error-fixes/assets/StandaloneFunctionContext.ets +116 -0
- package/skills/arkts-error-fixes/assets/StandaloneFunctionError.ets +85 -0
- package/skills/arkts-error-fixes/assets/StorageLinkDefaultError.ets +41 -0
- package/skills/arkts-error-fixes/assets/TitleButtonRectTypeError.ets +86 -0
- package/skills/arkts-error-fixes/assets/UnusedVariableWarning.ets +102 -0
- package/skills/arkts-error-fixes/assets/UtilityTypeError.ets +176 -0
- package/skills/arkts-error-fixes/assets/WindowRectSizeError.ets +65 -0
- package/skills/arkts-error-fixes/assets/WindowTypeError.ets +37 -0
- package/skills/arkts-error-fixes/reference/any_type_errors.md +433 -0
- package/skills/arkts-error-fixes/reference/appstorage_errors.md +95 -0
- package/skills/arkts-error-fixes/reference/arrow_function_conversion_errors.md +163 -0
- package/skills/arkts-error-fixes/reference/avoid_area_type_errors.md +59 -0
- package/skills/arkts-error-fixes/reference/breakpoint_type_errors.md +122 -0
- package/skills/arkts-error-fixes/reference/catch_clause_type_errors.md +89 -0
- package/skills/arkts-error-fixes/reference/color_consistency_errors.md +209 -0
- package/skills/arkts-error-fixes/reference/color_property_errors.md +136 -0
- package/skills/arkts-error-fixes/reference/context_type_errors.md +260 -0
- package/skills/arkts-error-fixes/reference/decorator_state_errors.md +109 -0
- package/skills/arkts-error-fixes/reference/display_listener_type_errors.md +149 -0
- package/skills/arkts-error-fixes/reference/duplicate_entry_errors.md +193 -0
- package/skills/arkts-error-fixes/reference/esobject_type_errors.md +110 -0
- package/skills/arkts-error-fixes/reference/fontcolor_property_errors.md +104 -0
- package/skills/arkts-error-fixes/reference/function_return_type_errors.md +194 -0
- package/skills/arkts-error-fixes/reference/idata_source_errors.md +77 -0
- package/skills/arkts-error-fixes/reference/implementation_not_allowed_errors.md +191 -0
- package/skills/arkts-error-fixes/reference/interface_method_signature_errors.md +143 -0
- package/skills/arkts-error-fixes/reference/notification_errors.md +110 -0
- package/skills/arkts-error-fixes/reference/object_literal_interface_errors.md +156 -0
- package/skills/arkts-error-fixes/reference/object_literal_type_errors.md +110 -0
- package/skills/arkts-error-fixes/reference/object_spread_errors.md +105 -0
- package/skills/arkts-error-fixes/reference/possibly_null_errors.md +156 -0
- package/skills/arkts-error-fixes/reference/resource_conversion_errors.md +154 -0
- package/skills/arkts-error-fixes/reference/standalone_function_errors.md +68 -0
- package/skills/arkts-error-fixes/reference/storage_link_default_errors.md +124 -0
- package/skills/arkts-error-fixes/reference/title_button_rect_type_errors.md +167 -0
- package/skills/arkts-error-fixes/reference/unused_variable_warnings.md +295 -0
- package/skills/arkts-error-fixes/reference/utility_type_errors.md +290 -0
- package/skills/arkts-error-fixes/reference/window_rect_size_errors.md +70 -0
- package/skills/arkts-error-fixes/reference/window_type_errors.md +95 -0
- package/skills/arkts-grammar-standards/SKILL.md +71 -0
- package/skills/arkts-grammar-standards/references/basic-syntax.md +103 -0
- package/skills/arkts-grammar-standards/references/restrictions.md +100 -0
- package/skills/arkts-grammar-standards/references/topic-aliases.json +93 -0
- package/skills/arkts-grammar-standards/references/ts-diff.md +108 -0
- package/skills/arkts-runtime-fix/SKILL.md +152 -0
- package/skills/arkts-runtime-fix/SKILL_CN.md +123 -0
- package/skills/arkts-runtime-fix/evals/evals.json +17 -0
- package/skills/arkts-runtime-fix/scripts/collect-hilog.mjs +116 -0
- package/skills/arkts-runtime-fix/scripts/collect-hilog.ts +115 -0
- package/skills/arkts-runtime-fix/scripts/fetch-faultlog.mjs +117 -0
- package/skills/arkts-runtime-fix/scripts/fetch-faultlog.ts +116 -0
- package/skills/arkts-runtime-fix/scripts/jscrash-report.mjs +149 -0
- package/skills/arkts-runtime-fix/scripts/parse-jscrash-log.mjs +126 -0
- package/skills/arkts-runtime-fix/scripts/parse-jscrash-log.ts +126 -0
- package/skills/arkts-runtime-fix/scripts/probe-faultlogger.mjs +160 -0
- package/skills/arkts-runtime-fix/scripts/probe-faultlogger.ts +160 -0
- package/skills/arkts-runtime-fix/scripts/shared/hdc.mjs +107 -0
- package/skills/arkts-runtime-fix/scripts/shared/hdc.ts +58 -0
- package/skills/arkts-runtime-fix/scripts/shared/jscrash-faultlogger.mjs +143 -0
- package/skills/arkts-runtime-fix/scripts/shared/jscrash-faultlogger.ts +148 -0
- package/skills/arkts-runtime-fix/scripts/shared/jscrash-parse.mjs +389 -0
- package/skills/arkts-runtime-fix/scripts/shared/jscrash-parse.ts +409 -0
- package/skills/arkts-runtime-fix/scripts/shared/utils.mjs +27 -0
- package/skills/arkts-runtime-fix/scripts/shared/utils.ts +27 -0
- package/skills/arkui-knowledge/SKILL.md +90 -0
- package/skills/arkui-knowledge/references/api-guardrails.md +60 -0
- package/skills/arkui-knowledge/references/common-mistakes.md +119 -0
- package/skills/arkui-knowledge/references/component-cookbook.md +135 -0
- package/skills/arkui-knowledge/references/ui-quality-checklist.md +36 -0
- package/skills/deveco-create-project/SKILL.md +144 -0
- package/skills/deveco-create-project/application/AppScope/app.json5 +10 -0
- package/skills/deveco-create-project/application/AppScope/resources/base/element/string.json +8 -0
- package/skills/deveco-create-project/application/AppScope/resources/base/media/background.png +0 -0
- package/skills/deveco-create-project/application/AppScope/resources/base/media/foreground.png +0 -0
- package/skills/deveco-create-project/application/AppScope/resources/base/media/layered_image.json +7 -0
- package/skills/deveco-create-project/application/build-profile.json5 +42 -0
- package/skills/deveco-create-project/application/code-linter.json5 +32 -0
- package/skills/deveco-create-project/application/entry/build-profile.json5 +33 -0
- package/skills/deveco-create-project/application/entry/hvigorfile.ts +7 -0
- package/skills/deveco-create-project/application/entry/obfuscation-rules.txt +23 -0
- package/skills/deveco-create-project/application/entry/oh-package.json5 +10 -0
- package/skills/deveco-create-project/application/entry/src/main/ets/entryability/EntryAbility.ets +63 -0
- package/skills/deveco-create-project/application/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets +31 -0
- package/skills/deveco-create-project/application/entry/src/main/ets/pages/Index.ets +38 -0
- package/skills/deveco-create-project/application/entry/src/main/module.json5 +50 -0
- package/skills/deveco-create-project/application/entry/src/main/resources/base/element/color.json +8 -0
- package/skills/deveco-create-project/application/entry/src/main/resources/base/element/float.json +8 -0
- package/skills/deveco-create-project/application/entry/src/main/resources/base/element/string.json +16 -0
- package/skills/deveco-create-project/application/entry/src/main/resources/base/media/background.png +0 -0
- package/skills/deveco-create-project/application/entry/src/main/resources/base/media/foreground.png +0 -0
- package/skills/deveco-create-project/application/entry/src/main/resources/base/media/layered_image.json +7 -0
- package/skills/deveco-create-project/application/entry/src/main/resources/base/media/startIcon.png +0 -0
- package/skills/deveco-create-project/application/entry/src/main/resources/base/profile/backup_config.json +3 -0
- package/skills/deveco-create-project/application/entry/src/main/resources/base/profile/main_pages.json +5 -0
- package/skills/deveco-create-project/application/entry/src/main/resources/dark/element/color.json +8 -0
- package/skills/deveco-create-project/application/hvigor/hvigor-config.json5 +23 -0
- package/skills/deveco-create-project/application/hvigorfile.ts +7 -0
- package/skills/deveco-create-project/application/oh-package.json5 +10 -0
- package/skills/deveco-create-project/scripts/copy-template.mjs +216 -0
- package/skills/deveco-create-project/scripts/copy-template.ts +214 -0
- package/skills/deveco-create-project/scripts/detect-sdk.mjs +82 -0
- package/skills/deveco-create-project/scripts/detect-sdk.ts +95 -0
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
# Interface Method Signature Errors
|
|
2
|
+
|
|
3
|
+
## Error: Interface method signature mismatch
|
|
4
|
+
|
|
5
|
+
### Error Message
|
|
6
|
+
```
|
|
7
|
+
Object literal must correspond to some explicitly declared class or interface
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
### Cause
|
|
11
|
+
When implementing an interface with an object literal, method signatures in the object must match the interface exactly. Using method syntax (`method() {}`) instead of property syntax (`method: () => {}`) causes type errors.
|
|
12
|
+
|
|
13
|
+
### Solution
|
|
14
|
+
Use property syntax for methods in object literals that implement interfaces. Change `method() {}` to `method: () => {}`.
|
|
15
|
+
|
|
16
|
+
### Key Points
|
|
17
|
+
- Use property syntax for methods in object literals
|
|
18
|
+
- Change `method(): ReturnType {}` to `method: () => ReturnType {}`
|
|
19
|
+
- Ensure parameter types match to interface
|
|
20
|
+
- Ensure return types match to interface
|
|
21
|
+
|
|
22
|
+
### Basic Pattern
|
|
23
|
+
```typescript
|
|
24
|
+
// ❌ Wrong: Method syntax in object literal
|
|
25
|
+
interface MyInterface {
|
|
26
|
+
updateWindowInfo(): void;
|
|
27
|
+
destroy(): void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const myObject: MyInterface = {
|
|
31
|
+
updateWindowInfo(): void {
|
|
32
|
+
console.log('Updated');
|
|
33
|
+
},
|
|
34
|
+
destroy(): void {
|
|
35
|
+
console.log('Destroyed');
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// ✅ Correct: Property syntax in object literal
|
|
40
|
+
interface MyInterface {
|
|
41
|
+
updateWindowInfo: () => void;
|
|
42
|
+
destroy: () => void;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const myObject: MyInterface = {
|
|
46
|
+
updateWindowInfo: () => {
|
|
47
|
+
console.log('Updated');
|
|
48
|
+
},
|
|
49
|
+
destroy: () => {
|
|
50
|
+
console.log('Destroyed');
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Common Patterns
|
|
56
|
+
```typescript
|
|
57
|
+
// No parameters
|
|
58
|
+
interface SimpleInterface {
|
|
59
|
+
doSomething: () => void;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const obj: SimpleInterface = {
|
|
63
|
+
doSomething: () => {
|
|
64
|
+
console.log('Doing something');
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// With parameters
|
|
69
|
+
interface ParameterInterface {
|
|
70
|
+
calculate: (a: number, b: number) => number;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const obj: ParameterInterface = {
|
|
74
|
+
calculate: (a: number, b: number): number => {
|
|
75
|
+
return a + b;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// With optional parameters
|
|
80
|
+
interface OptionalInterface {
|
|
81
|
+
process: (data: string, options?: Options) => void;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const obj: OptionalInterface = {
|
|
85
|
+
process: (data: string, options?: Options): void => {
|
|
86
|
+
console.log(data, options);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Detailed Examples
|
|
92
|
+
For more detailed code examples, see:
|
|
93
|
+
- [Interface Definition](../assets/InterfaceMethodSignatureError.ets#L1-L6)
|
|
94
|
+
- [Object Literal Implementation](../assets/InterfaceMethodSignatureError.ets#L8-L24)
|
|
95
|
+
- [Usage Pattern](../assets/InterfaceMethodSignatureError.ets#L26-L42)
|
|
96
|
+
|
|
97
|
+
### Best Practices
|
|
98
|
+
1. **Use property syntax**: Always use `method: () => {}` syntax for object literals
|
|
99
|
+
2. **Match signatures**: Ensure method signatures match to interface exactly
|
|
100
|
+
3. **Add type annotations**: Add explicit parameter and return types
|
|
101
|
+
4. **Keep interfaces simple**: Avoid overly complex method signatures
|
|
102
|
+
5. **Document interfaces**: Add comments explaining interface purpose
|
|
103
|
+
|
|
104
|
+
### Common Mistakes
|
|
105
|
+
```typescript
|
|
106
|
+
// ❌ Wrong: Method syntax in object literal
|
|
107
|
+
interface MyInterface {
|
|
108
|
+
updateWindowInfo(): void;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const obj: MyInterface = {
|
|
112
|
+
updateWindowInfo(): void {
|
|
113
|
+
console.log('Updated');
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
// ❌ Wrong: Method syntax in object literal
|
|
118
|
+
interface MyInterface {
|
|
119
|
+
updateWindowInfo: () => void;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const obj: MyInterface = {
|
|
123
|
+
updateWindowInfo(): void {
|
|
124
|
+
console.log('Updated');
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// ✅ Correct: Property syntax in object literal
|
|
129
|
+
interface MyInterface {
|
|
130
|
+
updateWindowInfo: () => void;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const obj: MyInterface = {
|
|
134
|
+
updateWindowInfo: () => {
|
|
135
|
+
console.log('Updated');
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Related Files
|
|
141
|
+
- [Code Example](../assets/InterfaceMethodSignatureError.ets)
|
|
142
|
+
- [Object Literal Interface Errors](./object_literal_interface_errors.md)
|
|
143
|
+
- [ArkTS Language Guide](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-get-started)
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# Notification API Type Errors
|
|
2
|
+
|
|
3
|
+
## Error: `notificationManager.ContentType` type incompatibility
|
|
4
|
+
|
|
5
|
+
### Error Message
|
|
6
|
+
```
|
|
7
|
+
Type 'notificationManager.ContentType' is not assignable to type 'notification.ContentType'
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
### Cause
|
|
11
|
+
Type mismatch between `notificationManager.ContentType` and `notification.ContentType`. The `notificationManager.ContentType` enum has additional properties that are not present in `notification.ContentType`, causing type incompatibility.
|
|
12
|
+
|
|
13
|
+
### Solution
|
|
14
|
+
Cast the ContentType value to `number` type to resolve the type incompatibility.
|
|
15
|
+
|
|
16
|
+
### Key Points
|
|
17
|
+
- Import from `@kit.NotificationKit`: `import { notificationManager } from '@kit.NotificationKit'`
|
|
18
|
+
- Cast to `number`: `as number` when assigning to `contentType`
|
|
19
|
+
- Use proper error handling with `BusinessError`
|
|
20
|
+
- Use `hilog` for logging instead of `console`
|
|
21
|
+
|
|
22
|
+
### Notification Request Structure
|
|
23
|
+
```typescript
|
|
24
|
+
interface NotificationRequest {
|
|
25
|
+
id: number;
|
|
26
|
+
content: NotificationContent;
|
|
27
|
+
notificationSlotType?: SlotType;
|
|
28
|
+
isOngoing?: boolean;
|
|
29
|
+
isUnremovable?: boolean;
|
|
30
|
+
deliveryTime?: number;
|
|
31
|
+
tapDismissed?: boolean;
|
|
32
|
+
autoDeletedTime?: number;
|
|
33
|
+
classification?: Classification;
|
|
34
|
+
groupName?: string;
|
|
35
|
+
slotType?: SlotType;
|
|
36
|
+
...
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Content Types
|
|
41
|
+
```typescript
|
|
42
|
+
notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT
|
|
43
|
+
notificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT
|
|
44
|
+
notificationManager.ContentType.NOTIFICATION_CONTENT_PICTURE
|
|
45
|
+
notificationManager.ContentType.NOTIFICATION_CONTENT_CONVERSATION
|
|
46
|
+
notificationManager.ContentType.NOTIFICATION_CONTENT_MULTILINE
|
|
47
|
+
notificationManager.ContentType.NOTIFICATION_CONTENT_MEDIA
|
|
48
|
+
notificationManager.ContentType.NOTIFICATION_CONTENT_LOCAL_LIVE_VIEW
|
|
49
|
+
notificationManager.ContentType.NOTIFICATION_CONTENT_LIVE_VIEW
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Basic Text Content
|
|
53
|
+
```typescript
|
|
54
|
+
interface NotificationBasicContent {
|
|
55
|
+
title: string;
|
|
56
|
+
text: string;
|
|
57
|
+
additionalText?: string;
|
|
58
|
+
largeIcon?: string;
|
|
59
|
+
briefText?: string;
|
|
60
|
+
expandedTitle?: string;
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Publishing Pattern
|
|
65
|
+
```typescript
|
|
66
|
+
let notificationRequest: notificationManager.NotificationRequest = {
|
|
67
|
+
id: 1,
|
|
68
|
+
content: {
|
|
69
|
+
contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT as number,
|
|
70
|
+
normal: {
|
|
71
|
+
title: 'Test Notification',
|
|
72
|
+
text: 'This is a test notification',
|
|
73
|
+
additionalText: 'Additional text'
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
notificationManager.publish(notificationRequest)
|
|
79
|
+
.then(() => {
|
|
80
|
+
hilog.info(0x0000, 'testTag', 'Publish notification success');
|
|
81
|
+
})
|
|
82
|
+
.catch((err: BusinessError) => {
|
|
83
|
+
hilog.error(0x0000, 'testTag', 'Publish notification failed: %{public}s', JSON.stringify(err));
|
|
84
|
+
});
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Error Handling
|
|
88
|
+
```typescript
|
|
89
|
+
// Common error codes
|
|
90
|
+
if (err.code === 1600001) {
|
|
91
|
+
// Notification not enabled
|
|
92
|
+
} else if (err.code === 1600002) {
|
|
93
|
+
// Too many notifications
|
|
94
|
+
} else if (err.code === 1600003) {
|
|
95
|
+
// Invalid notification
|
|
96
|
+
} else if (err.code === 1600004) {
|
|
97
|
+
// Notification slot not found
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Best Practices
|
|
102
|
+
1. **Cast to number**: Always cast ContentType to `number` to avoid type errors
|
|
103
|
+
2. **Use hilog**: Use `hilog` instead of `console` for better logging
|
|
104
|
+
3. **Handle errors**: Always catch and handle BusinessError
|
|
105
|
+
4. **Use unique IDs**: Ensure notification IDs are unique
|
|
106
|
+
5. **Test permissions**: Verify notification permissions are granted
|
|
107
|
+
|
|
108
|
+
### Related Files
|
|
109
|
+
- [Code Example](../assets/NotificationError.ets)
|
|
110
|
+
- [Notification Kit Documentation](https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-api-notificationmanager-V5)
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# Object Literal Interface Errors
|
|
2
|
+
|
|
3
|
+
## Error: `Object literal must correspond to some explicitly declared class or interface`
|
|
4
|
+
|
|
5
|
+
### Error Message
|
|
6
|
+
```
|
|
7
|
+
Object literal must correspond to some explicitly declared class or interface
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
### Cause
|
|
11
|
+
ArkTS has strict type checking for object literals. When you create an object literal, it must match an explicitly declared interface or class. This prevents type errors and ensures type safety.
|
|
12
|
+
|
|
13
|
+
### Solution
|
|
14
|
+
Define an interface or class that describes the shape of the object literal, then use that type for the object.
|
|
15
|
+
|
|
16
|
+
### Key Points
|
|
17
|
+
- Define interfaces for object shapes before using them
|
|
18
|
+
- Use the interface type for arrays of objects
|
|
19
|
+
- Ensure object literals match the interface exactly
|
|
20
|
+
- Use optional properties (`?`) for fields that may not be present
|
|
21
|
+
|
|
22
|
+
### Basic Pattern
|
|
23
|
+
```typescript
|
|
24
|
+
interface Article {
|
|
25
|
+
title: string;
|
|
26
|
+
desc: string;
|
|
27
|
+
image: Resource;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
@Entry
|
|
31
|
+
@Component
|
|
32
|
+
struct MyComponent {
|
|
33
|
+
private articles: Article[] = [
|
|
34
|
+
{ title: '文章1', desc: '这是文章1的描述', image: $r('app.media.article1') },
|
|
35
|
+
{ title: '文章2', desc: '这是文章2的描述', image: $r('app.media.article2') }
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
build() {
|
|
39
|
+
ForEach(this.articles, (article: Article) => {
|
|
40
|
+
Text(article.title)
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Interface Definition
|
|
47
|
+
```typescript
|
|
48
|
+
// Basic interface
|
|
49
|
+
interface User {
|
|
50
|
+
id: number;
|
|
51
|
+
name: string;
|
|
52
|
+
email: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Interface with optional properties
|
|
56
|
+
interface Config {
|
|
57
|
+
width: number;
|
|
58
|
+
height: number;
|
|
59
|
+
color?: string;
|
|
60
|
+
opacity?: number;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Interface with nested objects
|
|
64
|
+
interface Article {
|
|
65
|
+
title: string;
|
|
66
|
+
desc: string;
|
|
67
|
+
image: Resource;
|
|
68
|
+
metadata?: {
|
|
69
|
+
author: string;
|
|
70
|
+
date: string;
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Common Patterns
|
|
76
|
+
```typescript
|
|
77
|
+
// Configuration object
|
|
78
|
+
interface Config {
|
|
79
|
+
width: number;
|
|
80
|
+
height: number;
|
|
81
|
+
color: string;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const config: Config = { width: 100, height: 100, color: 'red' };
|
|
85
|
+
|
|
86
|
+
// User data
|
|
87
|
+
interface User {
|
|
88
|
+
id: number;
|
|
89
|
+
name: string;
|
|
90
|
+
email?: string;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const user: User = { id: 1, name: 'John' };
|
|
94
|
+
|
|
95
|
+
// API response
|
|
96
|
+
interface ApiResponse {
|
|
97
|
+
success: boolean;
|
|
98
|
+
data: any;
|
|
99
|
+
message?: string;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const response: ApiResponse = { success: true, data: {} };
|
|
103
|
+
|
|
104
|
+
// Breakpoint configuration
|
|
105
|
+
interface Breakpoint {
|
|
106
|
+
name: string;
|
|
107
|
+
range: [number, number];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const breakpoints: Breakpoint[] = [
|
|
111
|
+
{ name: 'sm', range: [320, 599] },
|
|
112
|
+
{ name: 'md', range: [600, 839] }
|
|
113
|
+
];
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Detailed Examples
|
|
117
|
+
For more detailed code examples, see:
|
|
118
|
+
- [Interface Definition](../assets/ObjectLiteralInterfaceError.ets#L1-L6)
|
|
119
|
+
- [Array of Objects](../assets/ObjectLiteralInterfaceError.ets#L8-L14)
|
|
120
|
+
- [ForEach Usage](../assets/ObjectLiteralInterfaceError.ets#L16-L48)
|
|
121
|
+
|
|
122
|
+
### Best Practices
|
|
123
|
+
1. **Define interfaces first**: Always define interfaces before using object literals
|
|
124
|
+
2. **Use descriptive names**: Interface names should clearly describe the object shape
|
|
125
|
+
3. **Make properties optional**: Use `?` for properties that may not be present
|
|
126
|
+
4. **Reuse interfaces**: Define interfaces once and reuse them throughout your code
|
|
127
|
+
5. **Document interfaces**: Add comments explaining the purpose of each interface
|
|
128
|
+
|
|
129
|
+
### Common Mistakes
|
|
130
|
+
```typescript
|
|
131
|
+
// ❌ Wrong: Object literal without interface
|
|
132
|
+
const articles = [
|
|
133
|
+
{ title: '文章1', desc: '描述', image: $r('app.media.icon') }
|
|
134
|
+
];
|
|
135
|
+
|
|
136
|
+
// ❌ Wrong: Array type with object literal
|
|
137
|
+
const articles: Array<{ title: string, desc: string, image: Resource }> = [
|
|
138
|
+
{ title: '文章1', desc: '描述', image: $r('app.media.icon') }
|
|
139
|
+
];
|
|
140
|
+
|
|
141
|
+
// ✅ Correct: Define interface first
|
|
142
|
+
interface Article {
|
|
143
|
+
title: string;
|
|
144
|
+
desc: string;
|
|
145
|
+
image: Resource;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const articles: Article[] = [
|
|
149
|
+
{ title: '文章1', desc: '描述', image: $r('app.media.icon') }
|
|
150
|
+
];
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Related Files
|
|
154
|
+
- [Code Example](../assets/ObjectLiteralInterfaceError.ets)
|
|
155
|
+
- [Object Spread Type Errors](./object_spread_errors.md)
|
|
156
|
+
- [ArkTS Language Guide](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-get-started)
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# Object Literal Type Declaration Error
|
|
2
|
+
|
|
3
|
+
## 错误描述
|
|
4
|
+
|
|
5
|
+
在 ArkTS 中,不能使用对象字面量作为类型声明。必须使用 `interface` 或 `class` 来定义类型。
|
|
6
|
+
|
|
7
|
+
## 错误示例
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
function getWindowSize(): { width: number; height: number } {
|
|
11
|
+
return { width: 100, height: 200 };
|
|
12
|
+
}
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
**错误信息:**
|
|
16
|
+
```
|
|
17
|
+
Object literals cannot be used as type declarations (arkts-no-obj-literals-as-types)
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## 解决方案
|
|
21
|
+
|
|
22
|
+
### 方案1:使用 interface(推荐)
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
interface WindowSize {
|
|
26
|
+
width: number;
|
|
27
|
+
height: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function getWindowSize(): WindowSize {
|
|
31
|
+
return { width: 100, height: 200 };
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### 方案2:使用 type 别名
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
type WindowSize = {
|
|
39
|
+
width: number;
|
|
40
|
+
height: number;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
function getWindowSize(): WindowSize {
|
|
44
|
+
return { width: 100, height: 200 };
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### 方案3:使用 class
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
class WindowSize {
|
|
52
|
+
width: number = 0;
|
|
53
|
+
height: number = 0;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function getWindowSize(): WindowSize {
|
|
57
|
+
const size = new WindowSize();
|
|
58
|
+
size.width = 100;
|
|
59
|
+
size.height = 200;
|
|
60
|
+
return size;
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## 详细说明
|
|
65
|
+
|
|
66
|
+
ArkTS 禁止使用对象字面量作为类型声明,这是为了:
|
|
67
|
+
|
|
68
|
+
1. **提高代码可读性**:使用命名的类型更清晰
|
|
69
|
+
2. **支持类型复用**:interface 和 type 可以在多个地方使用
|
|
70
|
+
3. **增强类型检查**:明确的类型定义可以提供更好的类型推断
|
|
71
|
+
|
|
72
|
+
## 简单示例
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
interface WindowSize {
|
|
76
|
+
width: number;
|
|
77
|
+
height: number;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
@Entry
|
|
81
|
+
@Component
|
|
82
|
+
struct WindowSizeExample {
|
|
83
|
+
@State windowSize: WindowSize = { width: 0, height: 0 };
|
|
84
|
+
|
|
85
|
+
aboutToAppear() {
|
|
86
|
+
this.windowSize = this.getWindowSize();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
private getWindowSize(): WindowSize {
|
|
90
|
+
const windowStage = this.getUIContext().getHostContext() as common.UIAbilityContext;
|
|
91
|
+
const windowClass = windowStage.getMainWindowSync();
|
|
92
|
+
const windowProperties = windowClass.getWindowProperties();
|
|
93
|
+
return {
|
|
94
|
+
width: windowProperties.windowRect.width,
|
|
95
|
+
height: windowProperties.windowRect.height
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
build() {
|
|
100
|
+
Column() {
|
|
101
|
+
Text(`宽度: ${this.windowSize.width}`)
|
|
102
|
+
Text(`高度: ${this.windowSize.height}`)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## 详细代码示例
|
|
109
|
+
|
|
110
|
+
> [ObjectLiteralTypeError.ets](../assets/ObjectLiteralTypeError.ets) - 完整的对象字面量类型声明错误示例和修复方案
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# Object Spread Type Errors
|
|
2
|
+
|
|
3
|
+
## Error: Object spread type inference issue
|
|
4
|
+
|
|
5
|
+
### Error Message
|
|
6
|
+
```
|
|
7
|
+
Type inference errors when using object spread
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
### Cause
|
|
11
|
+
ArkTS has strict type inference rules for object spread operations. Without explicit type annotations, the compiler cannot properly infer the resulting type.
|
|
12
|
+
|
|
13
|
+
### Solution
|
|
14
|
+
Explicitly type objects or use proper interface definitions before using spread operations.
|
|
15
|
+
|
|
16
|
+
### Key Points
|
|
17
|
+
- Define interfaces for object shapes
|
|
18
|
+
- Use explicit type annotations for spread results
|
|
19
|
+
- Ensure spread and target types are compatible
|
|
20
|
+
- Use optional properties for partial updates
|
|
21
|
+
|
|
22
|
+
### Interface Definition
|
|
23
|
+
```typescript
|
|
24
|
+
interface Config {
|
|
25
|
+
width: number;
|
|
26
|
+
height: number;
|
|
27
|
+
color?: string;
|
|
28
|
+
opacity?: number;
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Spread Pattern
|
|
33
|
+
```typescript
|
|
34
|
+
const baseConfig: Config = { width: 100, height: 100 };
|
|
35
|
+
const newConfig: Config = { ...baseConfig, color: 'red' };
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Partial Updates
|
|
39
|
+
```typescript
|
|
40
|
+
interface User {
|
|
41
|
+
id: number;
|
|
42
|
+
name: string;
|
|
43
|
+
email?: string;
|
|
44
|
+
phone?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const baseUser: User = { id: 1, name: 'John' };
|
|
48
|
+
const updatedUser: User = {
|
|
49
|
+
...baseUser,
|
|
50
|
+
email: 'john@example.com'
|
|
51
|
+
};
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Nested Objects
|
|
55
|
+
```typescript
|
|
56
|
+
interface NestedConfig {
|
|
57
|
+
layout: {
|
|
58
|
+
width: number;
|
|
59
|
+
height: number;
|
|
60
|
+
};
|
|
61
|
+
style: {
|
|
62
|
+
color: string;
|
|
63
|
+
opacity: number;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const base: NestedConfig = {
|
|
68
|
+
layout: { width: 100, height: 100 },
|
|
69
|
+
style: { color: 'red', opacity: 1.0 }
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const updated: NestedConfig = {
|
|
73
|
+
...base,
|
|
74
|
+
layout: { ...base.layout, width: 200 }
|
|
75
|
+
};
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Common Patterns
|
|
79
|
+
```typescript
|
|
80
|
+
// Configuration update
|
|
81
|
+
const config: Config = { ...defaultConfig, ...userConfig };
|
|
82
|
+
|
|
83
|
+
// State update
|
|
84
|
+
const newState: State = { ...currentState, ...updates };
|
|
85
|
+
|
|
86
|
+
// Merge defaults
|
|
87
|
+
const merged: Options = { ...defaultOptions, ...userOptions };
|
|
88
|
+
|
|
89
|
+
// Conditional spread
|
|
90
|
+
const result: Result = {
|
|
91
|
+
...base,
|
|
92
|
+
...(condition ? { extra: 'value' } : {})
|
|
93
|
+
};
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Best Practices
|
|
97
|
+
1. **Define interfaces**: Always define interfaces for object shapes
|
|
98
|
+
2. **Use explicit types**: Annotate spread results with interface types
|
|
99
|
+
3. **Optional properties**: Use optional properties for partial updates
|
|
100
|
+
4. **Type compatibility**: Ensure spread and target types are compatible
|
|
101
|
+
5. **Avoid deep spread**: Be careful with nested object spreads
|
|
102
|
+
|
|
103
|
+
### Related Files
|
|
104
|
+
- [Code Example](../assets/ObjectSpreadError.ets)
|
|
105
|
+
- [ArkTS Language Guide](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-get-started)
|