ArkTS Coding Standards
This skill provides guidelines and validation for writing correct ArkTS code, focusing on strict typing and performance optimizations required for HarmonyOS development.
❌ Illegal / Discouraged Practices
The following patterns are forbidden or highly discouraged in ArkTS:
Any/Unknown Types:
let x: any = 1; // ❌ Not allowed: `any` and `unknown` are forbidden.Object Literal Types:
type Inline = { value: number }; // ❌ Don't declare object-literal-based types.Fresh Object Literals (Untyped):
const bad: Inline = { value: 1 }; // ❌ Fresh object literal without declared class/interface instance.Runtime Shape Changes:
bad.newProp = "later"; // ❌ Changing object shape (adding properties) at runtime is forbidden.Implicit Object Shapes:
const obj = { value: 1, double() { return this.value + this.value; // ❌ `this` in a non-method context is disallowed. } }; // ❌ Even with an object literal, it must match an explicit class/interface.Type Mismatches:
bad.value = "7"; // ❌ Assigning string to a number-typed field fails.Implicit Coercion:
console.info(String(+bad.value)); // ❌ Unary `+` on a string for coercion is not permitted.
✅ Correct Practices
Use explicit types, classes, and safe conversions:
Explicit Numeric Parsing:
const s: string = "7"; // Use explicit parsing methods instead of unary + const n: number = Number.parseInt(s);Classes and Interfaces: Always define explicit classes or interfaces for your data structures.
class Doubler { value: number; constructor(value: number) { this.value = value; } } let obj = new Doubler(n);
Summary of Rules
- Static Typing: ArkTS is statically typed. Avoid dynamic features like
any. - Fixed Layout: Object layout is fixed at compile time. You cannot add/remove properties at runtime.
- Explicit Types: Define types explicitly using
classorinterface. Avoid anonymous object types. - Strict Safety: No implicit type coercion. Use standard library functions (e.g.,
Number.parseInt) for conversions.