21-ArkTs 常见错误

2025-03-31 23:58:39
149次阅读
0个评论

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());
收藏00

登录 后评论。没有帐号? 注册 一个。