DEV Community

Cover image for Adaptation rules from TypeScript to ArkTS
liu yang
liu yang

Posted on

Adaptation rules from TypeScript to ArkTS

ArkTS Constraints on TypeScript Features

Introduction

ArkTS imposes constraints on certain TypeScript features to enhance development correctness and runtime efficiency. This article lists the constrained features and provides code refactoring suggestions. ArkTS retains most TypeScript syntax, and code refactored according to this guide remains valid TypeScript code.

Example

Original TypeScript code using the var keyword:

function addTen(x: number): number {
  var ten = 10;
  return x + ten;
}
Enter fullscreen mode Exit fullscreen mode

Refactored code:

function addTen(x: number): number {
  let ten = 10;
  return x + ten;
}
Enter fullscreen mode Exit fullscreen mode

Constraint Levels

  • Errors: Constraints that must be followed. Violations will cause compilation failures.
  • Warnings: Recommended constraints. Violations currently do not affect compilation but may cause failures in the future.

Unsupported Features

Currently, unsupported features mainly include:

  • Features related to dynamic types that affect runtime performance.
  • Features requiring extra compiler support and increasing build time.

Mandatory Static Typing

ArkTS enforces static typing. All types must be known at compile - time, allowing developers to easily understand data structures and enabling the compiler to validate code early, reducing runtime type checks and improving performance.

Example

// Unsupported:
let res: any = some_api_function('hello', 'world');
// Supported:
class CallResult {
  public succeeded(): boolean { /* ... */ }
  public errorMessage(): string { /* ... */ }
}

let res: CallResult = some_api_function('hello', 'world');
if (!res.succeeded()) {
  console.log('Call failed: ' + res.errorMessage());
}
Enter fullscreen mode Exit fullscreen mode

The use of any is rare in TypeScript and can be prohibited with code - checking tools like ESLint. While eliminating any requires code refactoring, the effort is minimal and benefits overall performance.

Prohibition on Runtime Object Layout Changes

To achieve optimal performance, ArkTS prohibits changing object layouts at runtime, including adding or deleting properties/methods and assigning arbitrary - typed values to object properties.

Example

class Point {
  public x: number = 0;
  public y: number = 0;

  constructor(x: number, y: number) {
    this.x = x;
    this.y = y;
  }
}

// Deleting an object property is not allowed:
let p1 = new Point(1.0, 1.0);
delete p1.x; // Compilation error in both TypeScript and ArkTS
delete (p1 as any).x; // Compilation error in ArkTS

// Adding a new property to an object is not allowed:
let p2 = new Point(2.0, 2.0);
p2.z = 'Label'; // Compilation error in both TypeScript and ArkTS
(p2 as any).z = 'Label'; // Compilation error in ArkTS

// Using symbols to add properties is also prohibited:
let p3 = new Point(3.0, 3.0);
let prop = Symbol();
(p3 as any)[prop] = p3.x; // Compilation error in ArkTS
p3[prop] = p3.x; // Compilation error in both TypeScript and ArkTS

// Assigning values of other types to object properties is not allowed:
let p4 = new Point(4.0, 4.0);
p4.x = 'Hello!'; // Compilation error in both TypeScript and ArkTS
(p4 as any).x = 'Hello!'; // Compilation error in ArkTS

// Valid use of Point objects:
function distance(p1: Point, p2: Point): number {
  return Math.sqrt(
    (p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y)
  );
}
let p5 = new Point(5.0, 5.0);
let p6 = new Point(6.0, 6.0);
console.log('Distance between p5 and p6: ' + distance(p5, p6));
Enter fullscreen mode Exit fullscreen mode

Changing object layouts can confuse developers and increase runtime overhead. This constraint aligns with static typing principles and is supported by code - checking tools, leading to minimal code changes and performance improvements.

Restricted Operator Semantics

ArkTS restricts certain operator semantics to enhance code clarity and performance. For details, refer to the constraint specifications.

Example

// Unary operator '+' can only be applied to numeric types:
let t = +42;   // Valid operation
let s = +'42'; // Compile - time error
Enter fullscreen mode Exit fullscreen mode

This restriction reduces language complexity and eliminates unnecessary runtime overhead, affecting only a tiny fraction of codebases.

No Support for Structural Typing

TypeScript supports structural typing, but ArkTS does not.

Example

class T {
  public name: string = '';

  public greet(): void {
    console.log('Hello, ' + this.name);
  }
}

class U {
  public name: string = '';

  public greet(): void {
    console.log('Greetings, ' + this.name);
  }
}

let u: U = new T(); // Allowed?

function greeter(u: U) {
  console.log('To ' + u.name);
  u.greet();
}

let t: T = new T();
greeter(t); // Allowed?
Enter fullscreen mode Exit fullscreen mode

ArkTS does not support structural typing due to its complexity and the performance costs of runtime support. This decision ensures code clarity and performance.

Top comments (0)