DEV Community

Cover image for First encounter with the ArkTS language
liu yang
liu yang

Posted on • Edited on

First encounter with the ArkTS language

ArkTS: HarmonyOS's Preferred Primary Language for Application Development

Introduction to ArkTS

ArkTS is HarmonyOS's preferred primary language for application development, designed to build upon the TypeScript (TS) ecosystem while extending its capabilities. It maintains the essential style of TypeScript while enhancing static checking and analysis through standardized definitions. This approach not only boosts the stability and performance of program execution but also provides developers with a powerful toolset for building robust and efficient applications.

ArkTS is specifically tailored for HarmonyOS, leveraging the strengths of TypeScript while addressing the unique requirements of modern application development. By integrating seamlessly with the HarmonyOS ecosystem, ArkTS enables developers to create applications that fully utilize the platform's capabilities, from UI development to state management and beyond.

Key Enhancements in ArkTS

Starting from API version 10, ArkTS has significantly strengthened static checking and analysis through standardization. These enhancements set ArkTS apart from standard TypeScript in several key ways:

  1. Mandatory Static Typing: Static typing is a fundamental feature of ArkTS. Unlike dynamically typed languages, where variable types are determined at runtime, ArkTS requires that variable types be explicitly defined. This allows the compiler to verify code correctness before execution, reducing the need for runtime type checking and improving performance. For example, consider the following code snippet:
   let age: number = 25;
   let name: string = "Alice";
Enter fullscreen mode Exit fullscreen mode

In this example, the types of age and name are explicitly declared, ensuring that any operations involving these variables adhere to the specified types.

  1. No Object Layout Changes at Runtime: To achieve maximum performance, ArkTS enforces that object layouts remain unchanged during program execution. This means that once an object's structure is defined, it cannot be altered dynamically. This constraint ensures that the runtime environment can optimize memory usage and access patterns, leading to more efficient execution.

  2. Restricted Operator Semantics: ArkTS restricts the semantics of certain operators to improve performance and encourage clearer code. For instance, the unary plus operator (+) can only be applied to numbers, not other variable types. This restriction helps prevent type coercion errors and ensures that operations are performed as intended.

   let number = +25; // Valid
   let string = +"25"; // Invalid in ArkTS
Enter fullscreen mode Exit fullscreen mode
  1. No Support for Structural Typing: Structural typing, which allows objects to be considered compatible based on their structure rather than their declared types, is not supported in ArkTS. This decision is based on the complexity and potential performance implications of implementing structural typing in the language, compiler, and runtime. However, the HarmonyOS development team is open to reconsidering this feature based on practical needs and feedback from the developer community.

Capabilities Extended by ArkTS in UI Development Frameworks

ArkTS significantly enhances the capabilities of UI development frameworks, providing a robust foundation for creating dynamic and interactive user interfaces. Some of the key capabilities include:

  1. Basic Syntax: ArkTS defines a declarative syntax for UI description, custom component creation, and dynamic UI element extension. This syntax integrates seamlessly with the ArkUI development framework, leveraging system components and their related event and property methods. For example, you can define a simple UI component as follows:
   import { Component, Text } from '@arkui/components';

   @Component
   class HelloWorld extends Component {
     render() {
       return (
         <Text>Hello, World!</Text>
       );
     }
   }
Enter fullscreen mode Exit fullscreen mode

In this example, the HelloWorld component uses the Text component from the ArkUI framework to display a greeting.

  1. State Management: ArkTS offers multi-dimensional state management, allowing data associated with UI components to be used and passed between different component levels. This includes parent-child and grandparent-grandchild components, as well as globally across the application or even across devices. Depending on the data transmission mode, state management can be read-only unidirectional or mutable bidirectional. For example:
   import { Component, State } from '@arkui/components';

   @Component
   class Counter extends Component {
     @State count: number = 0;

     increment() {
       this.count++;
     }

     render() {
       return (
         <div>
           <Text>{this.count}</Text>
           <Button onClick={() => this.increment()}>Increment</Button>
         </div>
       );
     }
   }
Enter fullscreen mode Exit fullscreen mode

In this example, the Counter component maintains a state variable count and provides a method to increment it. The state is updated and reflected in the UI dynamically.

  1. Rendering Control: ArkTS provides powerful rendering control capabilities, including conditional rendering, loop rendering, and data lazy loading. Conditional rendering allows UI content to be rendered based on different application states. Loop rendering iterates over data from a data source, creating corresponding components during each iteration. Data lazy loading involves iterating over data from a data source on-demand, creating components as needed. For example:
   import { Component, Text, For } from '@arkui/components';

   @Component
   class ListComponent extends Component {
     items: string[] = ['Item 1', 'Item 2', 'Item 3'];

     render() {
       return (
         <div>
           <For each={this.items} as="item">
             <Text>{item}</Text>
           </For>
         </div>
       );
     }
   }
Enter fullscreen mode Exit fullscreen mode

In this example, the ListComponent uses the For component to iterate over the items array and render a Text component for each item.

Compatibility and Evolution

ArkTS is designed to be compatible with the TypeScript/JavaScript (TS/JS) ecosystem, allowing developers to use TS/JS for development or reuse existing code. This compatibility ensures that developers can leverage their existing knowledge and tools while transitioning to HarmonyOS development. For detailed information on HarmonyOS's support for TS/JS, refer to the compatibility constraints for TS/JS in the official HarmonyOS documentation.

Looking ahead, ArkTS will continue to evolve based on application development and runtime requirements. Future enhancements will include:

  • Enhanced Parallel and Concurrent Capabilities: ArkTS will introduce more advanced features for parallel and concurrent programming, enabling developers to build highly responsive and efficient applications.
  • Strengthened System-Level Types: The language will provide more robust type definitions for system-level operations, ensuring greater safety and reliability.
  • Distributed Development Paradigms: ArkTS will support distributed development paradigms, allowing developers to build applications that seamlessly integrate with HarmonyOS's distributed capabilities.

In conclusion, ArkTS is a powerful and evolving language that enhances the capabilities of TypeScript for HarmonyOS application development. By providing robust static typing, restricted operator semantics, and advanced UI development features, ArkTS enables developers to create high-performance, stable, and interactive applications. As HarmonyOS continues to grow and evolve, so too will ArkTS, ensuring that developers have the tools they need to build the next generation of applications.

Top comments (0)