ArkTS - HUAWEI Developers

account24arrow_back16arrow_downarrow_right16back18close18collapse16expand16headpicinfo28jumphover16jumpnormal16language24menu18menu24moresearch24user
img
ArkTS

ArkTS: programming language used in the HarmonyOS ecosystem.

%cke_4440.png%

ArkTS provides capabilities like declarative UI paradigm and state management, allowing you to build powerful applications at reduced complexity.

By retaining the basic syntax style of TypeScript and enhancing static checks and analysis through standardization, ArkTS enables the detection of more errors during the development phase before program execution, improving code robustness and runtime performance.

To address the limitations in concurrency support found in JavaScript and TypeScript, ArkTS has introduced enhanced concurrency programming APIs and capabilities.

ArkTS supports efficient interoperability with JavaScript/TypeScript and is compatible with the JavaScript/TypeScript ecosystem.

Design Philosophy of ArkTS

To better support the development and running of HarmonyOS applications, ArkTS enhances static checks and analysis based on TypeScript since HarmonyOS NEXT Developer Preview 0. This approach offers two key benefits: First, many errors can be detected at compile time rather than at runtime, significantly reducing the risk of runtime errors and enhancing program robustness. What's more, reducing runtime type checks lowers runtime overhead, boosting execution performance.

ArkTS retains most of the syntax features of TypeScript, making it easier for you to get started. This means that you only need to adapt a small portion of your existing standard TypeScript code to ArkTS syntax, while the majority of the code can be reused directly.

ArkTS supports efficient interoperability with standard JavaScript/TypeScript and is fully compatible with the JavaScript/TypeScript ecosystem. HarmonyOS also provides an execution environment for standard JavaScript/TypeScript. You can choose to reuse or develop code using standard JavaScript/TypeScript for easier compatibility with the existing ecosystem.

 Feature Differences Between ArkTS and TypeScript

ArkTS has standardized constraints on TypeScript features that are overly flexible and may compromise development correctness or introduce unnecessary runtime overhead. The following code snippets illustrate some of these constrained features.

%cke_6369.png%

1. No support for changing object layout at runtime

%cke_9771.png%

The provided TypeScript code snippet demonstrates how to change the layout of an object by adding and removing properties at runtime. Supporting such features at runtime incurs significant performance costs. As such, ArkTS does not support such changes.

In ArkTS, you can use optional properties or assign undefined to the properties as an alternative.

2. Typed object literals

%cke_12780.png%

The provided TypeScript code snippet shows a scenario without type annotations. If the compiler is unaware of the exact type of the variable point, it cannot deeply optimize the code due to the uncertain object layout, leading to performance bottlenecks. Additionally, without type annotations, property types are not restricted. For example, while point.x is typed as number, it can also be assigned other types, resulting in additional runtime checks and overhead.

In ArkTS, object literals must be annotated with types.

%cke_14731.png%

3. No support for structural typing

%cke_19818.png%

The provided TypeScript code snippet demonstrates the feature of structural typing. Given that ArkTS has adopted a nominal typing system, adding support for structural typing would introduce unnecessary complexity for implementation. In the example, although the parameter type of foo is declared as C, a variable of type D can also be passed. This flexibility may affect program correctness. Moreover, due to different layouts of types D and C, the c.s property in foo cannot be accessed based on a fixed offset, thereby causing runtime performance bottlenecks.

Collapse