Skip to content

Typescript Special Operators

  • TypeScript Special Operators & Wildcards Quick Reference
  • Marks properties or parameters as optional
interface User {
name?: string
};
function greet(name?: string) {}
  • Safely access nested properties
const city = user?.address?.city; // undefined if user or address is null
  • Provides default for null/undefined
const name = inputName ?? 'Anonymous'; // Uses 'Anonymous' only if inputName is null/undefined
  • Removes optionality
type Required<T> = {
[P in keyof T]-?: T[P]
}; // Makes all properties required
  • Asserts that a value is not null/undefined
const element = document.getElementById('myEl')!;
element.focus(); // No null check needed
  • Combines multiple types
type Admin = User & { permissions: string[] }; // Has both User properties and permissions
  • Represents one of several types
type ID = string | number;
let id: ID = 123;
id = 'abc';
  • Explicit type conversion
const length = (someValue as string).length;
const element = el as HTMLInputElement;
  • Gets keys of a type
const user = { name: 'John', age: 30 };
type UserKeys = keyof User; // 'name' | 'age'
// function getProp<T>(obj: T, key: keyof T) {}
  • Gets type of a value
const user = { name: 'John', age: 30 };
type UserType = typeof user; // { name: string; age: number }
  • Checks property existence
const user = { name: 'John', age: 30 };
if ('name' in user) {
console.log(user.name);
} // Type-safe property check
  • Constrains generic types
function logLength<T extends { length: number }>(obj: T) {}// T must have length property
  • Infers types in conditionals
type ArrayType<T> = T extends (infer U)[] ? U : never;
type Num = ArrayType<number[]>; // number
  • Infers types in conditionals
type ArrayType<T> = T extends (infer U)[] ? U : never;
type Num = ArrayType<number[]>; // number
  • Makes properties read-only
interface Point {
readonly x: number;
readonly y: number;
}
point.x = 5; // Error!
  • Removes readonly
type Mutable<T> = {
-readonly [P in keyof T]: T[P]
};// Makes all properties mutable
  • Validates without widening
const colors = ['red', 'green'] satisfies string[]; // Type is string[], not (string \| number)[]
  • Import/export all from module
import * as React from 'react';
export * from './utils';
  • Rest parameters or spread elements
function sum(...numbers: number[]) {} //Rest
const newArr = [...oldArr, newItem]; //Spread
  • Dynamic object properties
interface Dictionary {
[key: string]: string;
};
const dict: Dictionary = { hello: 'world' };
  • Generic type parameters
function identity<T>(value: T): T {};
type Response<T> = { data: T };
  • Metadata/annotation
@Component({ selector: 'app-root' })
class AppComponent {}
  • Class private fields
class User { #password: string; }// Truly private, not just TypeScript-private
  • Readable numbers
const million = 1_000_000;
const hex = 0xFF_FF_FF;
  • Comments
// This is a comment
const x = 5; // inline comment
  • Block comments
/* This is a`<br>`multi-line comment */