DEV Community

Cover image for Adaptation Rules from TypeScript to ArkTS (4)
liu yang
liu yang

Posted on

Adaptation Rules from TypeScript to ArkTS (4)

ArkTS Constraints on TypeScript Features

No Support for Conditional Types

  • Rule: arkts-no-conditional-types
  • Severity: Error

    • Description: ArkTS does not support conditional type aliases. Introduce new types with explicit constraints or rewrite logic using Object.
    • TypeScript Example:
type X<T> = T extends number ? T : never;
type Y<T> = T extends Array<infer Item> ? Item : never;
Enter fullscreen mode Exit fullscreen mode
  • ArkTS Example:
// Provide explicit constraints in type aliases
type X1<T extends number> = T;

// Rewrite with Object, with less type control and a need for more type checks
type X2<T> = Object;

// Item must be used as a generic parameter and correctly instantiable
type YI<Item, T extends Array<Item>> = Item;
Enter fullscreen mode Exit fullscreen mode

No Support for Field Declarations in Constructors

  • Rule: arkts-no-ctor-prop-decls
  • Severity: Error

    • Description: ArkTS does not support declaring class fields within constructors. Declare these fields within the class.
    • TypeScript Example:
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;
  }
}
Enter fullscreen mode Exit fullscreen mode
  • ArkTS Example:
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;
  }
}
Enter fullscreen mode Exit fullscreen mode

No Support for Constructor Signatures in Interfaces

  • Rule: arkts-no-ctor-signatures-iface
  • Severity: Error

    • Description: ArkTS does not support constructor signatures in interfaces. Use functions or methods instead.
    • TypeScript Example:
interface I {
  new (s: string): I;
}

function fn(i: I) {
  return new i('hello');
}
Enter fullscreen mode Exit fullscreen mode
  • ArkTS Example:
interface I {
  create(s: string): I;
}

function fn(i: I) {
  return i.create('hello');
}
Enter fullscreen mode Exit fullscreen mode

No Support for Index Access Types

  • Rule: arkts-no-aliases-by-index
  • Severity: Error

    • Description: ArkTS does not support index access types.

No Support for Field Access by Index

  • Rule: arkts-no-props-by-index
  • Severity: Error

    • Description: ArkTS does not support dynamic field declaration or access. You can only access fields declared in the class or inherited visible fields. Accessing other fields will result in a compile - time error.
    • TypeScript Example:
class Point {
  x: string = '';
  y: string = '';
}
let p: Point = { x: '1', y: '2' };
console.log(p['x']);

class Person {
  name: string = '';
  age: number = 0;
  [key: string]: string | number;
}

let person: Person = {
  name: 'John',
  age: 30,
  email: '***@example.com',
  phoneNumber: '18*********',
};
Enter fullscreen mode Exit fullscreen mode
  • ArkTS Example:
class Point {
  x: string = '';
  y: string = '';
}
let p: Point = { x: '1', y: '2' };
console.log(p.x);

class Person {
  name: string;
  age: number;
  email: string;
  phoneNumber: string;

  constructor(name: string, age: number, email: string, phoneNumber: string) {
    this.name = name;
    this.age = age;
    this.email = email;
    this.phoneNumber = phoneNumber;
  }
}

let person = new Person('John', 30, '***@example.com', '18*********');
console.log(person['name']); // Compile - time error
console.log(person.unknownProperty); // Compile - time error

let arr = new Int32Array(1);
arr[0];
Enter fullscreen mode Exit fullscreen mode

Top comments (0)