DEV Community

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

Posted on

Adaptation Rules from TypeScript to ArkTS (3)

ArkTS Constraints on TypeScript Features

Use class Instead of Types with Call Signatures

  • Rule: arkts-no-call-signatures
  • Severity: Error

  • Description: ArkTS does not support call signatures in object types. Instead of using a type with a call signature, define a class with an invoke method.

  • TypeScript Example:

type DescribableFunction = {
  description: string;
  (someArg: string): string; // call signature
};

function doSomething(fn: DescribableFunction): void {
  console.log(fn.description + " returned " + fn(""));
}
Enter fullscreen mode Exit fullscreen mode
  • ArkTS Example:
class DescribableFunction {
  description: string;

  constructor() {
    this.description = "desc";
  }

  public invoke(someArg: string): string {
    return someArg;
  }
}

function doSomething(fn: DescribableFunction): void {
  console.log(fn.description + " returned " + fn.invoke(""));
}

doSomething(new DescribableFunction());
Enter fullscreen mode Exit fullscreen mode

Use class Instead of Types with Construct Signatures

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

  • Description: ArkTS does not support construct signatures in object types. Instead of using a type with a construct signature, define a class.

  • TypeScript Example:

class SomeObject {}

type SomeConstructor = {
  new (s: string): SomeObject;
};

function fn(ctor: SomeConstructor) {
  return new ctor("hello");
}
Enter fullscreen mode Exit fullscreen mode
  • ArkTS Example:
class SomeObject {
  public f: string;

  constructor(s: string) {
    this.f = s;
  }
}

function fn(s: string): SomeObject {
  return new SomeObject(s);
}
Enter fullscreen mode Exit fullscreen mode

Only One Static Block Allowed

  • Rule: arkts-no-multiple-static-blocks
  • Severity: Error

  • Description: ArkTS does not allow multiple static blocks in a class. Merge multiple static blocks into a single one.

  • TypeScript Example:

class C {
  static s: string;

  static {
    C.s = "aa";
  }

  static {
    C.s = C.s + "bb";
  }
}
Enter fullscreen mode Exit fullscreen mode
  • ArkTS Example:
class C {
  static s: string;

  static {
    C.s = "aa";
    C.s = C.s + "bb";
  }
}
Enter fullscreen mode Exit fullscreen mode

No Support for Index Signatures

  • Rule: arkts-no-indexed-signatures
  • Severity: Error

  • Description: ArkTS does not support index signatures. Use arrays instead.

  • TypeScript Example:

interface StringArray {
  [index: number]: string;
}

function getStringArray(): StringArray {
  return ["a", "b", "c"];
}

const myArray: StringArray = getStringArray();
const secondItem = myArray[1];
Enter fullscreen mode Exit fullscreen mode
  • ArkTS Example:
class X {
  public f: string[] = [];
}

const myArray: X = new X();
myArray.f.push("a", "b", "c");
const secondItem = myArray.f[1];
Enter fullscreen mode Exit fullscreen mode

Use Inheritance Instead of Intersection Types

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

  • Description: ArkTS does not support intersection types. Use inheritance instead.

  • TypeScript Example:

interface Identity {
  id: number;
  name: string;
}

interface Contact {
  email: string;
  phoneNumber: string;
}

type Employee = Identity & Contact;
Enter fullscreen mode Exit fullscreen mode
  • ArkTS Example:
interface Identity {
  id: number;
  name: string;
}

interface Contact {
  email: string;
  phoneNumber: string;
}

interface Employee extends Identity, Contact {}
Enter fullscreen mode Exit fullscreen mode

No Support for this Type

  • Rule: arkts-no-typing-with-this
  • Severity: Error

  • Description: ArkTS does not support the this type. Use explicit concrete types instead.

  • TypeScript Example:

interface ListItem {
  getHead(): this;
}

class C {
  n: number = 0;

  m(c: this) {
    // ...
  }
}
Enter fullscreen mode Exit fullscreen mode
  • ArkTS Example:

interface ListItem {
getHead(): ListItem;
}

class C {
n: number = 0;

m(c: C) {
// ...
}
}


Enter fullscreen mode Exit fullscreen mode

Top comments (0)