mirror of
https://github.com/affaan-m/everything-claude-code.git
synced 2026-05-13 18:00:35 +08:00
6.0 KiB
6.0 KiB
paths
| paths | |||||
|---|---|---|---|---|---|
|
HarmonyOS / ArkTS Coding Style
This file extends common/coding-style.md with HarmonyOS and ArkTS-specific content.
ArkTS Language Constraints
ArkTS is a strict, statically-typed subset of TypeScript. Violating these constraints causes compilation failures.
Type System
- No
anyorunknowntypes - always use explicit types - No index access types - use type names directly
- No conditional type aliases or
inferkeyword - No intersection types - use inheritance
- No mapped types - use classes and regular idioms
- No
typeoffor type annotations - use explicit type declarations - No
as constassertions - use explicit type annotations - No structural typing - use inheritance, interfaces, or type aliases
- No TypeScript utility types except
Partial,Required,Readonly,Record - For
Record<K, V>, index expression type isV | undefined - Omit type annotations in
catchclauses (ArkTS does not supportany/unknown)
Functions & Classes
- No function expressions - use arrow functions
- No nested functions - use lambdas
- No generator functions - use
async/awaitfor multitasking - No
Function.apply,Function.call,Function.bind- follow traditional OOP forthis - No constructor type expressions - use lambdas
- No constructor signatures in interfaces or object types - use methods or classes
- No declaring class fields in constructors - declare in class body
- No
thisin standalone functions or static methods - only in instance methods - No
new.target - No definite assignment assertions (
let v!: T) - use initialized declarations - No class literals - introduce named class types
- No using classes as objects (assigning to variables) - class declarations introduce types, not values
- Only one static block per class - merge all static statements
Object & Property Access
- No dynamic field declaration or
obj["field"]access - useobj.fieldsyntax - No
deleteoperator - use nullable type withnullto mark absence - No prototype assignment - use classes and interfaces
- No
inoperator - useinstanceof - No reassigning object methods - use wrapper functions or inheritance
- No
Symbol()API (exceptSymbol.iterator) - No
globalThisor global scope - use explicit module exports/imports - No namespaces as objects - use classes or modules
- No statements inside namespaces - use functions
Destructuring & Spread
- No destructuring assignments or variable declarations - use intermediate objects and field-by-field access
- No destructuring parameter declarations - pass parameters directly, assign local names manually
- Spread operator only for expanding arrays (or array-derived classes) into rest parameters or array literals
Modules & Imports
- No
require()- use regularimportsyntax - No
export = ...- use normal export/import - No import assertions - imports are compile-time in ArkTS
- No UMD modules
- No wildcards in module names
- All
importstatements must appear before all other statements - TypeScript codebases must not depend on ArkTS codebases via import (reverse is supported)
Other Restrictions
- No
var- uselet - No
for...inloops - use regularforloops for arrays - No
withstatements - No JSX expressions
- No
#private identifiers - useprivatekeyword - No declaration merging (classes, interfaces, enums) - keep definitions compact
- No index signatures - use arrays
- Comma operator only in
forloops - Unary operators
+,-,~only for numeric types (no implicit string conversion) - Enum members: only same-type compile-time expressions for explicit initializers
- Function return type inference is limited - specify return types explicitly when calling functions with omitted return types
Object Literals
- Supported only when compiler can infer the corresponding class or interface
- NOT supported for:
any/Object/objecttypes, classes/interfaces with methods, classes with parameterized constructors, classes withreadonlyfields
Naming Conventions
- Variables / functions:
camelCase(e.g.,getUserInfo,goodsList) - Classes / interfaces:
PascalCase(e.g.,UserViewModel,IGoodsModel) - Constants:
UPPER_SNAKE_CASE(e.g.,MAX_PAGE_SIZE,COLOR_PRIMARY) - File names:
PascalCasefor components (e.g.,HomePage.ets),camelCasefor utilities
Formatting
- Prefer double quotes for strings
- Semicolons at end of statements
- Never use
var- preferconst, thenlet - All methods, parameters, return values must have complete type annotations
File Organization
- Component files (
.ets): one@ComponentV2per file - ViewModel files: one ViewModel class per file
- Model files: related data models may share a file
- Keep files under 400 lines; extract helpers for files approaching 800 lines
Comments
- File header:
@file(file purpose) +@author(developer), if the project already uses file headers - Public methods: JSDoc with
@param,@returns; add@examplefor complex methods - Match the project's existing documentation language; use English unless the repository has already standardized on Chinese comments
Error Handling
// Use try/catch with proper error handling
try {
const result = await riskyOperation()
return result
} catch (error) {
hilog.error(0x0000, 'TAG', 'Operation failed: %{public}s', error)
throw new Error('User-friendly error message')
}
Immutability
Follow the common immutability principles - create new objects instead of mutating:
// BAD: mutation
function updateUser(user: UserModel, name: string): UserModel {
user.name = name // direct mutation
return user
}
// GOOD: immutable - create new instance
function updateUser(user: UserModel, name: string): UserModel {
const updated = new UserModel()
updated.id = user.id
updated.name = name
updated.email = user.email
return updated
}