Typescript Special Operators
- TypeScript Special Operators & Wildcards Quick Reference
? - Optional
Section titled “? - Optional”- Marks properties or parameters as optional
interface User { name?: string};
function greet(name?: string) {}? - Optional Chaining
Section titled “? - Optional Chaining”- Safely access nested properties
const city = user?.address?.city; // undefined if user or address is null?? - Nullish Coalescing
Section titled “?? - Nullish Coalescing”- Provides default for null/undefined
const name = inputName ?? 'Anonymous'; // Uses 'Anonymous' only if inputName is null/undefined-? - Required Modifier
Section titled “-? - Required Modifier”- Removes optionality
type Required<T> = { [P in keyof T]-?: T[P]}; // Makes all properties required! - Non-null Assertion
Section titled “! - Non-null Assertion”- Asserts that a value is not null/undefined
const element = document.getElementById('myEl')!; element.focus(); // No null check needed& - Intersection Type
Section titled “& - Intersection Type”- Combines multiple types
type Admin = User & { permissions: string[] }; // Has both User properties and permissions| - Union Type
Section titled “| - Union Type”- Represents one of several types
type ID = string | number;let id: ID = 123;id = 'abc';as - Type Assertion
Section titled “as - Type Assertion”- Explicit type conversion
const length = (someValue as string).length;const element = el as HTMLInputElement;keyof - Keyof Operator
Section titled “keyof - Keyof Operator”- 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) {}typeof - Typeof Operator
Section titled “typeof - Typeof Operator”- Gets type of a value
const user = { name: 'John', age: 30 };type UserType = typeof user; // { name: string; age: number }in - In Operator
Section titled “in - In Operator”- Checks property existence
const user = { name: 'John', age: 30 };if ('name' in user) { console.log(user.name);} // Type-safe property checkextends - Constraint Operator
Section titled “extends - Constraint Operator”- Constrains generic types
function logLength<T extends { length: number }>(obj: T) {}// T must have length propertyinfer - Infer Keyword
Section titled “infer - Infer Keyword”- Infers types in conditionals
type ArrayType<T> = T extends (infer U)[] ? U : never;type Num = ArrayType<number[]>; // numberinfer - Infer Keyword
Section titled “infer - Infer Keyword”- Infers types in conditionals
type ArrayType<T> = T extends (infer U)[] ? U : never;type Num = ArrayType<number[]>; // numberreadonly - Readonly Modifier
Section titled “readonly - Readonly Modifier”- Makes properties read-only
interface Point { readonly x: number; readonly y: number;}point.x = 5; // Error!-readonly - Mutable Modifier
Section titled “-readonly - Mutable Modifier”- Removes readonly
type Mutable<T> = { -readonly [P in keyof T]: T[P]};// Makes all properties mutablesatisfies - Satisfies Operator
Section titled “satisfies - Satisfies Operator”- Validates without widening
const colors = ['red', 'green'] satisfies string[]; // Type is string[], not (string \| number)[]* - Import/Export Wildcard
Section titled “* - Import/Export Wildcard”- Import/export all from module
import * as React from 'react';export * from './utils';... - Rest/Spread
Section titled “... - Rest/Spread”- Rest parameters or spread elements
function sum(...numbers: number[]) {} //Restconst newArr = [...oldArr, newItem]; //Spread[] - Index Signature
Section titled “[] - Index Signature”- Dynamic object properties
interface Dictionary { [key: string]: string;};const dict: Dictionary = { hello: 'world' };<> - Type Parameters
Section titled “<> - Type Parameters”- Generic type parameters
function identity<T>(value: T): T {};type Response<T> = { data: T };@ - Decorator
Section titled “@ - Decorator”- Metadata/annotation
@Component({ selector: 'app-root' }) class AppComponent {}# - Private Field
Section titled “# - Private Field”- Class private fields
class User { #password: string; }// Truly private, not just TypeScript-private_ - Numeric Separator
Section titled “_ - Numeric Separator”- Readable numbers
const million = 1_000_000;const hex = 0xFF_FF_FF;// - Single-line Comment
Section titled “// - Single-line Comment”- Comments
// This is a commentconst x = 5; // inline comment/* */ - Multi-line Comment
Section titled “/* */ - Multi-line Comment”- Block comments
/* This is a`<br>`multi-line comment */