DEV Community

Cover image for Introduction to the ArkTS language(2)
liu yang
liu yang

Posted on

Introduction to the ArkTS language(2)

Types in ArkTS

Basic and Reference Types

Basic Types

Basic data types, such as number and string, represent single data types precisely. They allow direct storage and access of data, and comparisons are made directly between values.

Reference Types

Reference types in ArkTS include objects, arrays, and functions. These complex data structures are accessed via references. Objects and arrays can contain multiple values or key - value pairs, while functions encapsulate executable code logic. Reference types access data through pointers in memory, so modifying a reference affects the original data.

Number Type

ArkTS provides the number type for integers and floating - point numbers.

Integer Literals:

  • Decimal integers composed of sequences of digits. Examples: 0, 117, -345.
  • Hexadecimal integers starting with 0x or 0X, containing digits (0 - 9) and letters a - f or A - F. Examples: 0x1123, 0x00111, -0xF1A7.
  • Octal integers starting with 0o or 0O, containing only digits (0 - 7). Example: 0o777.
  • Binary integers starting with 0b or 0B, containing only digits 0 and 1. Examples: 0b11, 0b0011, -0b11.

Floating - Point Literals:

  • A decimal integer, which can be signed (with "+" or "-" prefix).
  • A decimal point (".").
  • A fractional part represented by a string of decimal digits.
  • An exponent part starting with "e" or "E", followed by a signed or unsigned integer.

Examples:

let n1 = 3.14;
let n2 = 3.141592;
let n3 = .5;
let n4 = 1e2;

function factorial(n: number): number {
  if (n <= 1) {
    return 1;
  }
  return n * factorial(n - 1);
}

factorial(n1);  // 7.660344000000002 
factorial(n2);  // 7.680640444893748 
factorial(n3);  // 1 
factorial(n4);  // 9.33262154439441e+157 
Enter fullscreen mode Exit fullscreen mode

The number type may lose precision with large integers. Use the bigInt type when precision is needed:

let bigIntger: BigInt = BigInt('999999999999999999999999999999999999999999999999999999999999');
console.log('bigIntger' + bigIntger.toString());
Enter fullscreen mode Exit fullscreen mode

Boolean Type

The boolean type consists of the logical values true and false and is commonly used in conditional statements:

let isDone: boolean = false;

// ...

if (isDone) {
  console.log('Done!');
}
Enter fullscreen mode Exit fullscreen mode

String Type

The string type represents a sequence of characters and can include escape characters. String literals are enclosed in single quotes (' ') or double quotes (" "), and template literals are enclosed in backticks ( ):

let s1 = 'Hello, world!\n';
let s2 = 'this is a string';
let a = 'Success';
let s3 = `The result is ${a}`;
Enter fullscreen mode Exit fullscreen mode

Void Type

The void type specifies that a function has no return value. It has only one value, void, and can be used for generic type parameters:

class Class<T> {
  //...
}
let instance: Class<void>;
Enter fullscreen mode Exit fullscreen mode

Object Type

The Object type is the base type for all reference types. Any value, including those of basic types (which are automatically boxed), can be assigned to a variable of type Object. The object type represents types other than the basic types.

Array Type

An array is an object composed of data that can be assigned to the element type specified in the array declaration. Arrays can be assigned using array compound literals (a list of zero or more expressions enclosed in square brackets, each representing an array element). The array length is determined by the number of elements, and the index of the first element is 0.

Example:

let names: string[] = ['Alice', 'Bob', 'Carol'];
Enter fullscreen mode Exit fullscreen mode

Enum Type

An enum type is a value type consisting of a predefined set of named values, called enumeration constants. When using enumeration constants, the enum type name must be used as a prefix:

enum ColorSet { Red, Green, Blue }
let c: ColorSet = ColorSet.Red;
Enter fullscreen mode Exit fullscreen mode

Constant expressions can be used to explicitly set the values of enumeration constants:

enum ColorSet { White = 0xFF, Grey = 0x7F, Black = 0x00 }
let c: ColorSet = ColorSet.Black;
Enter fullscreen mode Exit fullscreen mode

Union Type

A union type is a reference type composed of multiple types and includes all possible types of the variable.

class Cat {
  name: string = 'cat';
  // ...
}
class Dog {
  name: string = 'dog';
  // ...
}
class Frog {
  name: string = 'frog';
  // ...
}
type Animal = Cat | Dog | Frog | number;

let animal: Animal = new Cat();
animal = new Frog();
animal = 42;
Enter fullscreen mode Exit fullscreen mode

Different mechanisms can be used to obtain values of specific types within a union type:

class Cat { sleep() {}; meow() {} }
class Dog { sleep() {}; bark() {} }
class Frog { sleep() {}; leap() {} }

type Animal = Cat | Dog | Frog;

function foo(animal: Animal) {
  if (animal instanceof Frog) {
    animal.leap();  // animal is of type Frog here
  }
  animal.sleep(); // Animal has a sleep method
}
Enter fullscreen mode Exit fullscreen mode

Alias Type

Alias types provide names for anonymous types (arrays, functions, object literals, or union types) or alternative names for existing types:

type Matrix = number[][];
type Handler = (s: string, no: number) => string;
type Predicate<T> = (x: T) => boolean;
type NullableObject = Object | null;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)