一、不支持this类型
规则:arkts-no-typing-with-this
级别:错误
ArkTS不支持this类型,改用显式具体类型。
TypeScrip

interface ListItem {
  getHead(): this
}

class C {
  n: number = 0

  m(c: this) {
    // ...
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

ArkTS

interface ListItem {
  getHead(): ListItem
}

class C {
  n: number = 0

  m(c: C) {
    // ...
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

二、不支持条件类型
规则:arkts-no-conditional-types
级别:错误
ArkTS不支持条件类型别名,引入带显式约束的新类型,或使用Object重写逻辑。
不支持infer关键字。
TypeScript

type X<T> = T extends number ? T: never
type Y<T> = T extends Array<infer Item> ? Item: never
  • 1.
  • 2.

ArkTS

// 在类型别名中提供显式约束
type X1<T extends number> = T

// 用Object重写,类型控制较少,需要更多的类型检查以确保安全
type X2<T> = Object

// Item必须作为泛型参数使用,并能正确实例化
.type YI<Item, T extends Array<Item>> = Item
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

三、不支持在constructor中声明字段
规则:arkts-no-ctor-prop-decls
级别:错误
ArkTS不支持在constructor中声明类字段。在class中声明这些字段。
TypeScript

class Person {
  constructor(
    protected ssn: string,
    private firstName: string,
    private lastName: string
  ) {
    this.ssn = ssn;
    this.firstName = firstName;
    this.lastName = lastName;
  }

  getFullName(): string {
    return this.firstName + ' ' + this.lastName;
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

ArkTS

class Person {
  protected ssn: string
  private firstName: string
  private lastName: string

  constructor(ssn: string, firstName: string, lastName: string) {
    this.ssn = ssn;
    this.firstName = firstName;
    this.lastName = lastName;
  }

  getFullName(): string {
    return this.firstName + ' ' + this.lastName;
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

本文根据HarmonyOS NEXT Developer Beta1官方公开的开发文档整理而成。