arkts-no-destruct-params:函数参数禁止使用解构语法
解决 ArkTS 报错 Parameter destructuring is not supported (arkts-no-destruct-params),演示如何在函数和组件回调中规范接收具名参数对象。
- 规则码
- arkts-no-destruct-params
- 错误码
- 10605091
- 级别
- error
- 适用
- API 12+ (HarmonyOS NEXT)
- 更新
- 2026/12/02
一句话排查结论
将函数签名中的解构参数 function fn({ a, b }: Opts) 改为接收完整对象 function fn(opts: Opts),在函数体内通过 opts.a、opts.b 访问属性。
为什么在 ArkTS 中会报这个错
函数参数解构属于解构语法的延伸,在函数调用栈展开和参数类型校验时会引入额外开销,ArkTS 严格模式通过 arkts-no-destruct-params 统一要求使用普通具名形参。
错误写法示例(TypeScript 习惯)
interface HttpOptions {
url: string;
timeout?: number;
}
// 错误:在形参位置直接解构对象
function request({ url, timeout = 3000 }: HttpOptions): void {
console.info(url, timeout);
}
正确改法(HarmonyOS NEXT 严格模式)
interface HttpOptions {
url: string;
timeout?: number;
}
// 正确:接收完整对象并在函数体内读取属性
function request(options: HttpOptions): void {
const url: string = options.url;
const timeout: number = options.timeout ?? 3000;
console.info(url, timeout);
}
实际开发常见场景与避坑
- 数组高阶方法回调:例如
entries.map(([k, v]) => ...)会触发此错误,应改为entries.map((entry) => ...)后访问entry[0]与entry[1]。 - React/Vue 迁移习惯:前端开发者习惯在工具函数或渲染辅助函数里写
({ item, index }) => ...,迁移到 ArkTS@Builder时务必改成具名参数。
常见问题 FAQ
如何在不使用参数解构的情况下给配置项提供默认值?▾
将参数声明为具名 interface 或 class,在函数体内部通过空值合并运算符(options.timeout ?? 5000)或类字段默认值提供缺省值。
开发者资源