skills/arkts-static-spec/SKILL.md
ArkTS static language specification reference for OpenHarmony. Use this skill when reading or writing ArkTS (.ets), explaining ArkTS type-system and static-semantics rules, validating code against the ArkTS spec, investigating ArkTS compile-time behavior, or comparing ArkTS with TypeScript migration guidance.
npx skillsauth add openharmonyinsight/openharmony-skills arkts-static-specInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Complete reference for the ArkTS programming language, including official specification and TypeScript migration guide.
在使用本 skill 回答问题时,必须严格遵守以下原则:
严格按照 skill 文档内容回答
明确标注文档未说明的内容
将 ArkTS 视为独立的静态语言
本技能分为两个主要部分:
arkts-static-spec/
├── spec/ # ArkTS 语言规范
│ ├── types.md # 类型系统
│ ├── classes.md # 类声明
│ ├── expressions.md # 表达式和运算符
│ ├── statements.md # 语句和控制流
│ ├── generics.md # 泛型
│ ├── annotations.md # 注解和装饰器
│ ├── modules.md # 模块系统
│ ├── lexical.md # 词法结构
│ ├── names.md # 声明和作用域
│ ├── conversions.md # 类型转换
│ ├── interfaces.md # 接口
│ ├── enums.md # 枚举
│ ├── errors.md # 错误处理
│ ├── concurrency.md # 并发和异步
│ ├── stdlib.md # 标准库
│ └── experimental.md# 实验性特性
│
└── cookbook/ # TypeScript 迁移指南
├── index.md # 迁移指南总览
├── recipes.md # 详细迁移食谱(144+ 项)
└── compatibility.md # TypeScript vs ArkTS 兼容性详情
官方 ArkTS 语言规范参考,按主题组织。
| 文件 | 内容 | 适用场景 | |------|------|---------| | types.md | 类型系统、预定义类型 | 查询类型、类型推断、默认值 | | classes.md | 类声明、访问修饰符、继承 | 定义类、实现接口、继承 | | expressions.md | 运算符、优先级 | 表达式求值、运算符使用 | | statements.md | 控制流、循环、try-catch | if/else、for、while、异常处理 | | generics.md | 泛型类型和函数 | 编写泛型代码、类型约束 | | annotations.md | 装饰器和元数据 | 使用 @Decorator | | modules.md | Import/export、命名空间 | 模块组织、导入导出 | | lexical.md | 标识符、关键字、字面量 | 词法分析、语法规则 | | names.md | 声明、作用域、可见性 | 变量作用域、命名冲突 | | conversions.md | 类型转换和上下文 | 类型转换规则 | | interfaces.md | 接口声明和实现 | 定义接口、实现契约 | | enums.md | 枚举类型 | 使用枚举 | | errors.md | 错误处理和 try 语句 | 异常处理、throw 语句 | | concurrency.md | Async/await、TaskPool | 异步编程、并发 | | stdlib.md | 标准库 API | 内置对象和函数 | | experimental.md | 实验性特性 | FixedArray、char、函数重载等 |
从 TypeScript 迁移到 ArkTS 的完整指南,包含 144+ 个详细迁移食谱和兼容性说明。
| 文件 | 内容 | 适用场景 | |------|------|---------| | index.md | 迁移指南总览 | 了解迁移原因、设计原则、快速入门 | | recipes.md | 144+ 详细迁移食谱 | 查询具体语法如何转换、常见问题解决 | | compatibility.md | TypeScript vs ArkTS 兼容性详情 | 了解行为差异、数值语义、数组赋值等 |
let num: number = 42
const str: string = "hello"
let arr: number[] = [1, 2, 3]
class Person {
private name: string
public age: number
constructor(name: string, age: number) {
this.name = name
this.age = age
}
public introduce(): string {
return `I'm ${this.name}, ${this.age} years old`
}
}
function identity<T>(value: T): T {
return value
}
let num = identity<number>(42)
let str = identity<string>("hello")
async function fetchData(): Promise<string> {
const response = await fetch("https://example.com")
return await response.text()
}
| 分类 | 类型 | 说明 |
|------|------|------|
| 数值 | byte, short, int, long, float, double, number, bigint | 整数和浮点数 |
| 布尔 | boolean | true/false |
| 字符 | char | 实验性,使用 c'A' 语法 |
| 字符串 | string | 文本数据 |
| 特殊 | void, null, undefined, never | 特殊用途 |
| 对象 | Object, object | 对象类型 |
注意:any 和 unknown 在 ArkTS 中不支持,必须使用显式类型。
T | UT & UT[] 或 Array<T>readonly T[]| TypeScript | ArkTS | 说明 |
|-----------|-------|------|
| var x = 10 | let x: number = 10 | 必须使用 let,明确类型 |
| let x: any | let x: SpecificType | 禁止 any,使用具体类型 |
| let {a, b} = obj | let a = obj.a; let b = obj.b | 不支持对象解构 |
| "prop" in obj | obj instanceof Class | 不支持 in 运算符 |
| import m = require("mod") | import * as m from "mod" | 使用 ES6 import |
根据主题选择对应的规范文件:
| 特性 | TypeScript | ArkTS |
|------|-----------|-------|
| 静态类型 | 可选 | 强制 |
| any 类型 | 允许 | 禁止 |
| unknown 类型 | 允许 | 禁止 |
| 类型推断 | 可选 | 必须 |
| 特性 | TypeScript | ArkTS |
|------|-----------|-------|
| 运行时添加属性 | 允许(用 as any 绕过) | 禁止 |
| 删除属性 | 允许(用 as any 绕过) | 禁止 |
| 对象布局 | 可变 | 固定 |
| 特性 | TypeScript | ArkTS |
|------|-----------|-------|
| null 赋值给非空类型 | 可选模式下禁止 | 强制禁止 |
| undefined 作为值 | 允许 | 严格限制 |
let n = 1
console.log(n / 2)
// TypeScript: 0.5(浮点除法)
// ArkTS: 0(整数除法)
| 修饰符 | 可访问性 |
|--------|---------|
| public | 任何地方(默认) |
| private | 仅类内部 |
| protected | 类和子类 |
| readonly | 初始化后不可变 |
| static | 类级别,非实例级别 |
class Singleton {
private static instance: Singleton
private constructor() {}
public static getInstance(): Singleton {
if (!Singleton.instance) {
Singleton.instance = new Singleton()
}
return Singleton.instance
}
}
interface Product {
operation(): string
}
class ConcreteProductA implements Product {
operation(): string {
return "Product A"
}
}
class Factory {
static create(type: string): Product {
if (type === "A") {
return new ConcreteProductA()
}
throw new Error("Unknown product type")
}
}
程序稳定性
程序性能
代码可读性
any、使用 let 代替 varif (condition) {
// ...
} else {
// ...
}
// while 循环
while (condition) {
// ...
}
// for 循环
for (let i: int = 0; i < 10; i++) {
// ...
}
// for-of 循环
for (const item of array) {
// ...
}
try {
// 可能抛出错误的代码
} catch (e: Error) {
// 处理错误
} finally {
// 清理(总是执行)
}
var 为 letany / unknown 类型Symbol() API 使用# 标识符in 运算符require 为标准 importexport = 语法D:\arkcompiler\runtime_core\static_core\plugins\ets\doc\spec\D:\arkcompiler\runtime_core\static_core\plugins\ets\doc\cookbook\development
Run local code quality checks covering a subset of OpenHarmony gate CI (copyright, CodeArts C/C++) plus additional local checks (pylint/flake8, shellcheck/bashate, gn format). Use before committing to reduce gate failures. Triggers on: /oh-precommit-codecheck, "门禁检查", "门禁预检", "检查代码", "run codecheck", "check code quality", "lint my code", "代码检查", or after completing code implementation. WHEN to use: before git commit, before creating PR, after modifying C/C++/Python/Shell/GN files, when gate CI fails with codecheck defects, or when you want to preview what gate will flag.
development
OpenHarmony PR full lifecycle workflow. Five modes: - Commit: standardized commit with DCO sign-off and Issue linking - Create PR: commit + push to fork + create Issue + create PR on upstream - Fix Codecheck: fetch gate CI codecheck defects from a PR and auto-fix them - Review PR: fetch a PR's changes to local for code review - Fix Review: fetch unresolved review comments from a PR and auto-fix them Triggers on: /oh-pr-workflow, "提交代码", "创建PR", "提个PR", "commit", "修复告警", "修复门禁", "修复codecheck", "fix codecheck", "review pr", "review这个pr", "看下这个pr", "检视pr", "修复review", "修复检视意见", "fix review", or a GitCode PR URL with fix/review intent.
testing
分析 HM Desktop PRD 文档,提取需求信息、验证完整性、检查章节顺序(需求来源→需求背景→需求价值分析→竞品分析→需求描述)、检查 KEP 定义、检测需求冲突并生成结构化分析报告。适用于用户请求:(1) 分析或审查 PRD 文档, (2) 从需求中提取 KEP 列表, (3) 检查 PRD 完整性或一致性, (4) 将需求映射到模块架构, (5) 验证 PRD 格式合规性, (6) 验证竞品分析章节完整性。关键词:PRD分析, requirement extraction, KEP验证, completeness check, chapter order validation, 竞品分析检查, analyze PRD, 需求提取, 完整性检查, 章节顺序验证
development
基于 PRD 文档自动生成鸿蒙系统设计文档,包括架构设计文档和功能设计文档。生成前会分析 OpenHarmony 存量代码结构,确保与现有架构兼容。架构设计文档第2章必须为竞品方案分析,位于需求背景之后。适用于用户请求:(1) 生成架构设计文档, (2) 生成功能设计文档, (3) 从 PRD 生成设计文档, (4) 创建系统架构设计, (5) 编写功能规格说明, (6) 分析 OH 代码结构。关键词:architecture design, functional design, design doc, 竞品方案分析, OpenHarmony code analysis, 架构设计, 功能设计, 设计文档生成, OH代码分析, analyze codebase, competitor analysis