Essential TypeScript Operators and Syntax Features

Non-null Assertion Operator (!)

The postfix ! operator tells the type checker that a value is not null or undefined, even if the type system can't confirm it. This is useful when you're certain a value exists but TypeScript's static analysis isn't aware.

const processName = (input: string | null | undefined) => {
  const safeName: string = input!; // Asserts non-null/undefined
};

It also applies to function calls:

type Callback = () => void;

const invoke = (fn: Callback | null | undefined) => {
  fn!(); // Safe call assuming fn exists
};

Optional Chaining (?.)

Optional chaining safely accesses nested properties with out throwing errors if an intermediate property is null or undefined. The expression short-circuits and returns undefined in such cases.

const data = {
  user: {
    profile: {
      id: 101
    }
  }
};

const userId = data?.user?.profile?.id;     // 101
const email = data?.user?.contact?.email;   // undefined

Nullish Coalescing (??)

The ?? operator returns the right-hand operand only when the left-hand operand is null or undefined. Unlike ||, it doesn't treat other falsy values (like 0, '', or false) as triggers for fallback.

const displayName = null ?? 'Guest';        // 'Guest'
const count = 0 ?? 10;                      // 0
const countFallback = 0 || 10;              // 10

Optional Properties (?)

In interfaces or types, a property followed by ? indicates it may be omitted.

interface User {
  username: string;
  age?: number; // Optional
}

const user1: User = { username: 'alice' }; // Valid

Intersection Types (&)

The & operator combines multiple types into one that includes all properties from each.

type HasX = { x: number };
type HasY = { y: number };
type Point = HasX & HasY;

const p: Point = { x: 5, y: -2 };

Union Types (|)

Union types allow a value to be one of several specified types.

function logId(id: string | number | null) {
  console.log(id);
}

Numeric Separators (_)

Underscores improve readability of large numeric literals without affecting their value.

const billion = 1_000_000_000;
const hexColor = 0xFF_00_FF;
// Equivalent to 1000000000 and 0xFF00FF

Type Assertion

Type asertions inform the compiler about a value’s type when you have more context than it does. Two syntaxes exist:

Angle bracket syntax:

let input: any = "hello";
let len = (<string>input).length;

as syntax (preferred in TSX):

let len = (input as string).length;

Private Class Fields (#)

Private fields in classes are denoted with a # prefix. They are truly private—unavailable outside the class and not detectable via reflection.

class Vehicle {
  #engineStatus: boolean;

  constructor(status: boolean) {
    this.#engineStatus = status;
  }

  start() {
    if (!this.#engineStatus) {
      console.log('Engine started');
      this.#engineStatus = true;
    }
  }
}

const car = new Vehicle(false);
// car.#engineStatus; // Compile error

Tags: TypeScript Operators type-system Syntax

Posted on Sat, 26 Sep 2026 16:25:40 +0000 by tidou