TypeScript Fundamentals for HarmonyOS Development

TypeScript Language Overview

TypeScript is a statically typed superset of JavaScript that compiles to plain JavaScript. It adds optional types, classes, and modules to JavaScript, helping catch errors early through static type checikng and making code more maintainable.

In the HarmonyOS ecosystem, ArkTS builds upon TypeScript by extending it with declarative UI capabilities, state management, and concurrency features tailored for cross-platform application development.

TypeScript Basic Syntax

Boolean and Number Types

// Boolean type variables
let isEnabled: boolean = true;
let isDisabled: boolean = false;

console.log(isEnabled);
console.log(isDisabled);

// Number type variables
let decimalValue: number = 12;
let floatValue: number = 3.14;
let binaryValue: number = 0b110;

console.log(decimalValue);
console.log(floatValue);
console.log(binaryValue);

Binary literals are automatically converted to decimal when printed to the console.

String and Array Types

// String type variable
let greeting: string = "hello";

// Array type declaration - first method
let numberArray: number[] = [1, 2, 3, 4, 6, 10];
console.log(numberArray);
console.log(numberArray[4]); // Access array element by index

// Array type declaration - second method
let stringArray: Array<string> = ['a', 'b', 'd'];
console.log(stringArray);

// Array manipulation
stringArray.push("hello"); // Add element to end
console.log(stringArray);

stringArray.pop(); // Remove last element
console.log(stringArray);

stringArray.splice(2, 0, "hello"); // Insert at position 2
console.log(stringArray);

stringArray.splice(1, 1); // Remove element at position 1
console.log(stringArray);

Arrays in TypeScript support various manipulation methods including push, pop, and splice for modifiyng content.

Enum Type

enum Direction {
    North,
    South,
    East,
    West
}

let currentDirection: Direction = Direction.North;
console.log(currentDirection); // Outputs: 0

Unknown and Void Types

// Unknown type for variables of uncertain type
let uncertainValue: unknown = "This could be anything";
console.log(uncertainValue);

// Void type for functions that return nothing
function logMessage(message: string): void {
    console.log(message);
}

logMessage("Function with void return type");

Union Types

// Variable that can hold multiple types
let flexibleValue: string | number;
flexibleValue = "Text value";
console.log(flexibleValue);

flexibleValue = 42;
console.log(flexibleValue);

Tags: TypeScript HarmonyOS ArkTS javascript StaticTyping

Posted on Mon, 28 Sep 2026 16:34:54 +0000 by ohjay