DEV Community

liu yang
liu yang

Posted on

Introduction to the ArkTS language(8)

Null Safety

By default, all types in ArkTS are non - nullable, meaning their values cannot be null. This is similar to TypeScript's strictNullChecks mode but with stricter rules.

In the following example, all lines will result in compile - time errors:

let x: number = null;    // Compile-time error
let y: string = null;    // Compile-time error
let z: number[] = null;  // Compile-time error
Enter fullscreen mode Exit fullscreen mode

Variables that can hold null values are defined as union types T | null:

let x: number | null = null;
x = 1;    // OK
x = null; // OK
if (x != null) { /* do something */ }
Enter fullscreen mode Exit fullscreen mode

Non - null Assertion Operator

The postfix operator! can be used to assert that its operand is non - null. When applied to a value of a nullable type, its compile - time type becomes non - null. For example, the type changes from T | null to T:

class A {
  value: number = 0;
}

function foo(a: A | null) {
  a.value;   // Compile-time error: Cannot access property of nullable value
  a!.value;  // Compilation succeeds; if a is non - null at runtime, access to a's property is possible; if a is null at runtime, a runtime exception occurs
}
Enter fullscreen mode Exit fullscreen mode

Nullish Coalescing Operator

The nullish coalescing binary operator ?? checks whether the evaluation of the left - hand expression is null or undefined. If so, the result of the expression is the right - hand expression; otherwise, the result is the left - hand expression.

In other words, a ?? b is equivalent to the ternary operator (a != null && a != undefined) ? a : b.

In the following example, the getNick method returns the nickname if it is set; otherwise, it returns an empty string:

class Person {
  // ...
  nick: string | null = null;
  getNick(): string {
    return this.nick ?? '';
  }
}
Enter fullscreen mode Exit fullscreen mode

Optional Chaining

When accessing object properties, if the property is undefined or null, the optional chaining operator returns undefined.

class Person {
  nick: string | null = null;
  spouse?: Person;

  setSpouse(spouse: Person): void {
    this.spouse = spouse;
  }

  getSpouseNick(): string | null | undefined {
    return this.spouse?.nick;
  }

  constructor(nick: string) {
    this.nick = nick;
    this.spouse = undefined;
  }
}
Enter fullscreen mode Exit fullscreen mode

Note: The return type of getSpouseNick must be string | null | undefined because the method may return null or undefined.

Optional chaining can be arbitrarily long and can include any number of ?. operators.

In the following example, if an instance of Person has a non - null spouse property and the spouse has a non - null nick property, spouse.nick is output. Otherwise, undefined is output:

class Person {
  nick: string | null = null;
  spouse?: Person;

  constructor(nick: string) {
    this.nick = nick;
    this.spouse = undefined;
  }
}

let p: Person = new Person('Alice');
p.spouse?.nick; // undefined
Enter fullscreen mode Exit fullscreen mode

Top comments (0)