HarmonyOS 组件参数类型校验怎么做才对?TypeUtil 全面实战
文章目录背景方法总览综合 TypeUtil 所有方法场景一处理 ResourceStr 类型参数场景二正确处理 catch 块的错误场景三判断函数是否异步场景四处理二进制数据一张图总结 TypeUtil 的使用决策树写在最后背景近期发现一款很有意思的HarmonyOS 三方库, 地址 pura/harmony-utils(V1.4.0) , 作者是桃花镇童长老, 我这里也是直接通过该作者公布的源码进行案例编写进行,写了到目前写了一部分demo ,感觉确实很有帮助,这里呢也是开始写一个系列的演示demo 供大家参考。如有帮助可以在OpenHarmony中进行下载安装进行使用哦案例demo导航展示↓↓↓↓↓↓接下来言归正传 ↓↓↓↓写通用组件的时候最头疼的问题之一是参数可能传各种类型如何在运行时做精确判断ArkTS 的类型系统在编译时很严格但运行时的类型判断还是需要TypeUtil来帮忙。方法总览综合 TypeUtil 所有方法先快速回顾 TypeUtil Demo 里覆盖的所有方法基础类型TypeUtil.isBoolean(true)// → trueTypeUtil.isBoolean(true)// → falseTypeUtil.isNumber(42)// → trueTypeUtil.isNumber(42)// → falseTypeUtil.isString(hello)// → trueTypeUtil.isString(123)// → falseTypeUtil.isObject({a:1})// → trueTypeUtil.isArray([1,2,3])// → trueTypeUtil.isArray({})// → falseTypeUtil.isFunction((){})// → trueHarmonyOS Resource 类型TypeUtil.isResource($r(app.string.app_name))// → trueTypeUtil.isResource(plain string)// → falseTypeUtil.isResourceStr(text)// → trueTypeUtil.isResourceStr($r(app.string.app_name))// → true容器类型TypeUtil.isMap(newMap())// → trueTypeUtil.isMap({})// → falseTypeUtil.isWeakMap(newWeakMap())// → trueTypeUtil.isSet(newSet([1,2]))// → trueTypeUtil.isWeakSet(newWeakSet())// → true特殊对象TypeUtil.isDate(newDate())// → trueTypeUtil.isDate(2024-01-01)// → falseTypeUtil.isRegExp(/abc/g)// → trueTypeUtil.isDataView(newDataView(buf))// → trueTypeUtil.isArrayBuffer(newArrayBuffer(4))// → trueTypeUtil.isUint8Array(newUint8Array())// → trueTypeUtil.isTypedArray(newFloat32Array())// → true异步与错误TypeUtil.isPromise(Promise.resolve())// → trueTypeUtil.isAsyncFunction(async(){})// → trueTypeUtil.isAsyncFunction((){})// → falseTypeUtil.isNativeError(newError(err))// → trueTypeUtil.isNativeError(string)// → false场景一处理 ResourceStr 类型参数HarmonyOS 开发中经常遇到ResourceStr string | Resource类型的参数需要分别处理// 判断是 Resource 对象this.Btn(isResource($r(app.string.app_name)) → true,#7B68EE,(){constres$r(app.string.app_name);this.addLog(isResource($r(app.string.app_name)) →${TypeUtil.isResource(res)});})this.Btn(isResource(plain string) → false,#7B68EE,(){this.addLog(isResource(plain string) →${TypeUtil.isResource(plain string)});})// ResourceStr 接受两种this.Btn(isResourceStr(text) → true,#6C5CE7,(){this.addLog(isResourceStr(text) →${TypeUtil.isResourceStr(text)});})this.Btn(isResourceStr($r(app.string.app_name)) → true,#6C5CE7,(){constres$r(app.string.app_name);this.addLog(isResourceStr($r(...)) →${TypeUtil.isResourceStr(res)});})场景二正确处理 catch 块的错误this.Btn(isNativeError(new Error(err)) → true,#C0392B,(){this.addLog(isNativeError(new Error()) →${TypeUtil.isNativeError(newError(err))});})this.Btn(isNativeError(string) → false,#C0392B,(){this.addLog(isNativeError(string) →${TypeUtil.isNativeError(string)});})实际业务里的健壮错误处理functionhandleError(e:unknown):string{if(TypeUtil.isNativeError(e)){// 标准 Error 对象访问 message 和 stackreturn(easError).message;}elseif(TypeUtil.isString(e)){// 字符串类型的错误returneasstring;}else{// 其他类型returnJSON.stringify(e);}}场景三判断函数是否异步this.Btn(isAsyncFunction(async (){}) → true,#6C3483,(){constfnasync(){};this.addLog(isAsyncFunction(async (){}) →${TypeUtil.isAsyncFunction(fn)});})this.Btn(isAsyncFunction((){}) → false,#6C3483,(){constfn(){};this.addLog(isAsyncFunction((){}) →${TypeUtil.isAsyncFunction(fn)});})场景四处理二进制数据this.Btn(isArrayBuffer(new ArrayBuffer(4)) → true,#2C3E50,(){this.addLog(isArrayBuffer(new ArrayBuffer(4)) →${TypeUtil.isArrayBuffer(newArrayBuffer(4))});})this.Btn(isUint8Array(new Uint8Array()) → true,#34495E,(){this.addLog(isUint8Array(new Uint8Array()) →${TypeUtil.isUint8Array(newUint8Array())});})this.Btn(isTypedArray(new Float32Array()) → true,#626567,(){this.addLog(isTypedArray(new Float32Array()) →${TypeUtil.isTypedArray(newFloat32Array())});})一张图总结 TypeUtil 的使用决策树需要判断类型 ├── 是基础类型boolean/number/string/object/array/function │ → 用 isBoolean/isNumber/isString/isObject/isArray/isFunction ├── 是 HarmonyOS Resource 类型 │ → 用 isResource 或 isResourceStr ├── 是容器类型Map/Set/WeakMap/WeakSet │ → 用对应的 isMap/isSet/isWeakMap/isWeakSet ├── 是特殊对象Date/RegExp/DataView │ → 用 isDate/isRegExp/isDataView ├── 是二进制数据ArrayBuffer/TypedArray │ → 用 isArrayBuffer/isUint8Array/isTypedArray 等 ├── 是异步相关Promise/async函数 │ → 用 isPromise/isAsyncFunction └── 是错误对象 → 用 isNativeError写在最后TypeUtil 覆盖了 HarmonyOS ArkTS 开发中几乎所有需要精确类型检测的场景。从简单的isString、isArray到 HarmonyOS 特有的isResource再到isPromise、isNativeError——基本上你能想到的类型判断需求都有对应方法。建议在项目的工具库里引入 TypeUtil把它当成typeof的升级替代品来使用。