What is ArkTS? The language behind OpenHarmony and HarmonyOS apps

Published:

arktslanguagebasics

Short answer: ArkTS is the preferred programming language for OpenHarmony and HarmonyOS application development. It is built on TypeScript and keeps its basic style, but adds stricter rules, such as mandatory static typing, so the code can be checked at compile time and run faster.

What is ArkTS?

According to the official OpenHarmony documentation, ArkTS “extends” the TypeScript ecosystem while maintaining the basic style of TypeScript. It stays compatible with the TypeScript and JavaScript ecosystems where possible, and it improves static checking and analysis during development, which makes programs more stable and faster.

You use ArkTS together with ArkUI, the declarative UI framework, inside DevEco Studio.

How is ArkTS different from TypeScript?

The documented key characteristics (API version 10 and later) are:

FeatureWhat it means
Mandatory static typingTypes are known at compile time, so the compiler verifies the code and less checking is needed at run time.
Fixed object layoutAn object’s structure cannot change while the program runs.
Restricted operatorsOperator behavior is limited for clarity and performance. For example, the unary plus is restricted to numeric types.
No structural typingNot supported, because of implementation complexity.

In practice this means that some dynamic TypeScript patterns, such as the any type, are not allowed in ArkTS. Follow the official TypeScript-to-ArkTS adaptation rules when porting code.

What does ArkTS code look like?

The syntax is familiar if you know TypeScript. Variables use let and const:

let hi: string = 'hello';
const hello: string = 'hello';

Arrays, enums and union types:

let names: string[] = ['Alice', 'Bob', 'Carol'];
enum ColorSet { Red, Green, Blue }
type Animal = Cat | Dog | Frog | number | string | null;

Functions and classes declare their types explicitly:

function add(x: string, y: string): string {
  return `${x} ${y}`;
}

class Person {
  public name: string = '';
  constructor(n: string) {
    this.name = n;
  }
}

How does null safety work in ArkTS?

All types are non-nullable by default. To allow null, use a union type, and use optional chaining to access properties safely:

let x: number | null = null;
// return this.spouse?.nick;

How do you build a UI with ArkTS and ArkUI?

A page is a struct with the @Component decorator and a build() method. @Entry marks the page as an entry, and @State makes a value reactive, so the UI updates when it changes:

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .height('100%')
  }
}

Build and run this in your first project: Your first ArkTS app.

Next steps

Frequently asked questions

Is ArkTS the same as TypeScript?

No. ArkTS is built on TypeScript and keeps its basic style, but adds restrictions for performance and stability: mandatory static typing, a fixed object layout, restricted operator semantics and no structural typing.

Can I use my existing TypeScript or JavaScript code in ArkTS?

Often only after changes. The official documentation provides adaptation rules for moving TypeScript code to ArkTS, because ArkTS forbids some dynamic features.

What is ArkUI, and how does it relate to ArkTS?

ArkUI is the declarative UI framework. You write ArkUI interfaces in ArkTS using decorators such as @Entry, @Component and @State and a build() method.

Do I need to learn TypeScript first?

It helps a lot, since ArkTS keeps TypeScript's syntax. Knowing types, classes and interfaces covers most of the language basics.