Adaptation Rules from TypeScript to ArkTS (2)

ArkTS Constraints on TypeScript Features Object Property Names Must Be Valid Identifiers Rule: arkts-identifiers-as-prop-names Severity: Error Description: In ArkTS, object property names cannot be numbers or arbitrary strings. Exceptions are string literals and string values in enums. Use property names to access class properties and numeric indices for array elements. TypeScript Example: var x = { 'name': 'x', 2: '3' }; console.log(x['name']); console.log(x[2]); ArkTS Example: class X { public name: string = ''; } let x: X = { name: 'x' }; console.log(x.name); let y = ['a', 'b', 'c']; console.log(y[2]); // Use Map for non - identifier keys let z = new Map(); z.set('name', '1'); z.set(2, '2'); console.log(z.get('name')); console.log(z.get(2)); enum Test { A = 'aaa', B = 'bbb' } let obj: Record = { [Test.A]: 1, // String value from enum [Test.B]: 2, // String value from enum ['value']: 3 // String literal } No Support for Symbol() API Rule: arkts-no-symbol Severity: Error Description: ArkTS does not support the Symbol() API due to its limited relevance in a statically - typed language. Object layout is determined at compile - time and cannot be changed at runtime. Only Symbol.iterator is supported. No Private Fields with # Prefix Rule: arkts-no-private-identifiers Severity: Error Description: ArkTS does not support private fields declared with the # prefix. Use the private keyword instead. TypeScript Example: class C { #foo: number = 42; } ArkTS Example: class C { private foo: number = 42; } Unique Names for Types and Namespaces Rule: arkts-unique-names Severity: Error Description: Types (classes, interfaces, enums), and namespaces must have unique names that do not conflict with other identifiers like variable or function names. TypeScript Example: let X: string; type X = number[]; // Type alias shares name with variable ArkTS Example: let X: string; type T = number[]; // Renamed to avoid conflict Use let Instead of var Rule: arkts-no-var Severity: Error Description: ArkTS prefers let for variable declaration due to its block - scope and reduced error risk. TypeScript Example: function f(shouldInitialize: boolean) { if (shouldInitialize) { var x = 'b'; } return x; } console.log(f(true)); // b console.log(f(false)); // undefined let upperLet = 0; { var scopedVar = 0; let scopedLet = 0; upperLet = 5; } scopedVar = 5; // Visible scopedLet = 5; // Compile - time error ArkTS Example: function f(shouldInitialize: boolean): string { let x: string = 'a'; if (shouldInitialize) { x = 'b'; } return x; } console.log(f(true)); // b console.log(f(false)); // a let upperLet = 0; let scopedVar = 0; { let scopedLet = 0; upperLet = 5; } scopedVar = 5; scopedLet = 5; // Compile - time error Explicit Types Instead of any or unknown Rule: arkts-no-any-unknown Severity: Error Description: ArkTS does not support the any and unknown types. Declare variables with explicit types. TypeScript Example: let value1: any; value1 = true; value1 = 42; let value2: unknown; value2 = true; value2 = 42; ArkTS Example: let value_b: boolean = true; // Or let value_b = true let value_n: number = 42; // Or let value_n = 42 let value_o1: Object = true; let value_o2: Object = 42;

Jun 12, 2025 - 17:10
 0
Adaptation Rules from TypeScript to ArkTS (2)

ArkTS Constraints on TypeScript Features

Object Property Names Must Be Valid Identifiers

  • Rule: arkts-identifiers-as-prop-names
  • Severity: Error

    • Description: In ArkTS, object property names cannot be numbers or arbitrary strings. Exceptions are string literals and string values in enums. Use property names to access class properties and numeric indices for array elements.
    • TypeScript Example:
var x = { 'name': 'x', 2: '3' };

console.log(x['name']);
console.log(x[2]);
  • ArkTS Example:
class X {
  public name: string = '';
}
let x: X = { name: 'x' };
console.log(x.name);

let y = ['a', 'b', 'c'];
console.log(y[2]);

// Use Map for non - identifier keys
let z = new Map<Object, string>();
z.set('name', '1');
z.set(2, '2');
console.log(z.get('name'));
console.log(z.get(2));

enum Test {
  A = 'aaa',
  B = 'bbb'
}

let obj: Record<string, number> = {
  [Test.A]: 1,   // String value from enum
  [Test.B]: 2,   // String value from enum
  ['value']: 3   // String literal
}

No Support for Symbol() API

  • Rule: arkts-no-symbol
  • Severity: Error

    • Description: ArkTS does not support the Symbol() API due to its limited relevance in a statically - typed language. Object layout is determined at compile - time and cannot be changed at runtime. Only Symbol.iterator is supported.

No Private Fields with # Prefix

  • Rule: arkts-no-private-identifiers
  • Severity: Error

    • Description: ArkTS does not support private fields declared with the # prefix. Use the private keyword instead.
    • TypeScript Example:
class C {
  #foo: number = 42;
}
  • ArkTS Example:
class C {
  private foo: number = 42;
}

Unique Names for Types and Namespaces

  • Rule: arkts-unique-names
  • Severity: Error

    • Description: Types (classes, interfaces, enums), and namespaces must have unique names that do not conflict with other identifiers like variable or function names.
    • TypeScript Example:
let X: string;
type X = number[]; // Type alias shares name with variable
  • ArkTS Example:
let X: string;
type T = number[]; // Renamed to avoid conflict

Use let Instead of var

  • Rule: arkts-no-var
  • Severity: Error

    • Description: ArkTS prefers let for variable declaration due to its block - scope and reduced error risk.
    • TypeScript Example:
function f(shouldInitialize: boolean) {
  if (shouldInitialize) {
    var x = 'b';
  }
  return x;
}

console.log(f(true));  // b
console.log(f(false)); // undefined

let upperLet = 0;
{
  var scopedVar = 0;
  let scopedLet = 0;
  upperLet = 5;
}
scopedVar = 5; // Visible
scopedLet = 5; // Compile - time error
  • ArkTS Example:
function f(shouldInitialize: boolean): string {
  let x: string = 'a';
  if (shouldInitialize) {
    x = 'b';
  }
  return x;
}

console.log(f(true));  // b
console.log(f(false)); // a

let upperLet = 0;
let scopedVar = 0;
{
  let scopedLet = 0;
  upperLet = 5;
}
scopedVar = 5;
scopedLet = 5; // Compile - time error

Explicit Types Instead of any or unknown

  • Rule: arkts-no-any-unknown
  • Severity: Error

    • Description: ArkTS does not support the any and unknown types. Declare variables with explicit types.
    • TypeScript Example:
let value1: any;
value1 = true;
value1 = 42;

let value2: unknown;
value2 = true;
value2 = 42;
  • ArkTS Example:
let value_b: boolean = true; // Or let value_b = true
let value_n: number = 42; // Or let value_n = 42
let value_o1: Object = true;
let value_o2: Object = 42;