21-ArkTs 常见错误

21-ArkTs 常见错误

arkts-no-ctor-signatures-funcs

函数返回类型不匹配

应用代码

class Test {  handleClick: (action: string, externInfo?: string) => void | null = null;}

建议改法

在这种写法下,函数返回类型被解析为 void | undefined,需要添加括号用来区分union类型。

class Test {  handleClick: ((action: string, externInfo?: string) => void) | null = null;}

‘***’ is of type ‘unknown’

应用代码

try {  } catch (error) {  console.log(error.message);}

建议改法

import { BusinessError } from '@kit.BasicServicesKit'
try {  } catch (error) {  console.log((error as BusinessError).message);}

Type ‘*** | null’ is not assignable to type ‘***’

应用代码

class A {  value: number  constructor(value: number) {    this.value = value;  }}
function foo(v: number): A | null {  if (v > 0) {    return new A(v);  }  return null;}
let a: A = foo();

建议改法1

修改变量a的类型:let a: A | null = foo()。

class A {  value: number  constructor(value: number) {    this.value = value;  }}
function foo(v: number): A | null {  if (v > 0) {    return new A(v);  }  return null;}
let a: A | null = foo(123);
if (a != null) {  // 非空分支} else {  // 处理null}

建议改法2

如果可以断定此处调用foo一定返回非空值,可以使用非空断言!。

class A {  value: number  constructor(value: number) {    this.value = value;  }}
function foo(v: number): A | null {  if (v > 0) {    return new A(v);  }  return null;}
let a: A = foo(123)!;

Cannot invoke an object which possibly ‘undefined’

应用代码

interface A {  foo?: () => void}
let a:A = { foo: () => {} };a.foo();

建议改法1

interface A {  foo: () => void}let a: A = { foo: () => {} };a.foo();

建议改法2

interface A {  foo?: () => void}
let a: A = { foo: () => {} };if (a.foo) {  a.foo();}

原因

在原先代码的定义中,foo是可选属性,有可能为undefined,对undefined的调用会导致报错。建议按照业务逻辑判断是否需要为可选属性。如果确实需要,那么在访问到该属性后需要进行空值检查。

Variable ‘***’ is used before being assigned

应用代码

class Test {  value: number = 0}
let a: Testtry {  a = { value: 1};} catch (e) {  a.value;}a.value;

建议改法

class Test {  value: number = 0}
let a: Test | null = null;try {  a = { value:1 };} catch (e) {  if (a) {    a.value;  }}
if (a) {  a.value;}

原因

对于primitive types,可以根据业务逻辑赋值,例如0,‘’,false。

对于对象类型,可以将类型修改为和null的联合类型,并赋值null,使用时需要进行非空检查。

Function lacks ending return statement and return type does not include ‘undefined’.

应用代码

function foo(a: number): number {  if (a > 0) {    return a;  }}

建议改法1

根据业务逻辑,在else分支中返回合适的数值

建议改法2

function foo(a: number): number | undefined {  if (a > 0) {    return a;  }  return}

arkts-strict-typing-required

应用代码

// @ts-nocheckvar a: any = 123;

建议改法

let a: number = 123;

原因

ArkTS不支持通过注释的方式绕过严格类型检查。首先将注释(// @ts-nocheck或者// @ts-ignore)删去,再根据报错信息修改其他代码。

Importing ArkTS files to JS and TS files is not allowed

arkts-no-tsdeps

不允许.ts、.js文件import.ets文件源码。

建议改法

方式1.将.ts文件的后缀修改成ets,按照ArkTS语法规则适配代码。

方式2.将.ets文件中被.ts文件依赖的代码单独抽取到.ts文件中。

arkts-no-special-imports

应用代码

import type {A, B, C, D } from '***'

建议改法

import {A, B, C, D } from '***'

arkts-no-classes-as-obj

使用class构造实例

应用代码

class Controller {  value: string = ''  constructor(value: string) {    this.value = value  }}
interface ControllerConstructor {  new (value: string): Controller;}
class Menu {  controller: ControllerConstructor = Controller  createController() {    if (this.controller) {      return new this.controller('abc');    }    return null;  }}
let t = new Menu();console.log(t.createController()!.value);

建议改法

class Controller {  value: string = ''  constructor(value: string) {    this.value = value  }}
type ControllerConstructor = () => Controller;
class Menu {  controller: ControllerConstructor = () => { return new Controller('abc'); }  createController() {    if (this.controller) {      return this.controller();    }    return null;  }}
let t: Menu = new Menu();console.log(t.createController()!.value);

访问静态属性

应用代码

class C1 {  static value: string = 'abc'}
class C2 {  static value: string = 'def'}
function getValue(obj: any) {  return obj['value'];}
console.log(getValue(C1));console.log(getValue(C2));

建议改法

class C1 {  static value: string = 'abc'}
class C2 {  static value: string = 'def'}
function getC1Value(): string {  return C1.value;}
function getC2Value(): string {  return C2.value;}
console.log(getC1Value());console.log(getC2Value());
### 关于 ArkTS 标准库使用限制 ArkTS 是基于 TypeScript 扩展而来的一种编程语言,在 HarmonyOS 平台上具有特定的应用场景。尽管大部分 TypeScript 的语法和标准库功能可以在 ArkTS 中正常使用,然而由于 HarmonyOS 平台的独特性质,某些标准库的功能可能会受到限制或者不完全兼容[^2]。 这些限制主要体现在以下几个方面: - **API 可用性**:并非所有的 JavaScript/TypeScript API 都能在 ArkTS 环境下运行良好; - **性能优化**:为了适应移动设备资源有限的特点,一些高消耗的操作被禁用了; - **安全考量**:出于安全性考虑,部分可能导致安全隐患的标准库方法也被屏蔽掉了; 当开发者遇到 `arkts-limited-stdlib` 类型的错误提示时,通常意味着当前尝试调用的方法或属性并不适用于该环境下的 ArkTS 版本。 ### 处理 arkts-limited-stdlib 错误解决方案 对于此类问题的一个有效解决办法是在项目配置文件中调整依赖项版本号来匹配最新的 SDK 或者适配器包。具体操作如下所示: 如果正在使用的 Deveco Studio 工具链出现了上述提到的问题,则可以通过更新工具链至最新稳定版的方式解决问题。此外,还可以通过修改项目的 tsconfig.json 文件中的 compilerOptions 来指定更合适的 lib 字段值,从而避开那些不受支持的部分[^1]。 ```json { "compilerOptions": { ... "lib": ["esnext", "dom"], ... } } ``` 另外一种常见的做法就是寻找官方文档提供的替代实现方式或者是社区贡献的相关 polyfill 库来进行补充。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

万少-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值