规则:arkts-no-generators
级别:错误
目前ArkTS不支持生成器函数,使用async或await机制进行并行任务处理。
TypeScript
function* counter(start: number, end: number) {
for (let i = start; i <= end; i++) {
yield i;
}
}
for (let num of counter(1, 5)) {
console.log(num);
}
ArkTS
async function complexNumberProcessing(str: string): Promise<string> {
// ...
return str;
}
async function foo() {
for (let i = 1; i <= 5; i++) {
console.log(await complexNumberProcessing(i));
}
}
foo()
使用instanceof和as进行类型保护
规则:arkts-no-is
级别:错误
ArkTS不支持is运算符,必须用instanceof运算符替代。在使用之前,必须使用as运算符将对象转换为需要的类型。
TypeScript
class Foo {