TypeScript Keywords

Essential keywords for TypeScript development

🔑 What are TypeScript Keywords?

TypeScript keywords are reserved words with special meanings. They define types, control access, and structure your code. Understanding keywords is essential for writing effective TypeScript applications.


// Common TypeScript keywords
interface User {
  readonly id: number
  name: string
}

type Status = 'active' | 'inactive'
                                    

Essential Keywords

📦

interface

Define object structure

interface Person {
  name: string
  age: number
}
🏷️

type

Create type aliases

type ID = 
  string | number

type Point = {
  x: number, y: number
}
🔒

readonly

Make properties immutable

interface Config {
  readonly apiKey: string
}

optional (?)

Mark properties as optional

interface User {
  name: string
  email?: string
}

🔹 Type Definition Keywords

Keywords for defining custom types:

// interface - Define object shapes
interface Product {
  id: number
  name: string
  price: number
}

// type - Create type aliases
type Status = 'pending' | 'approved' | 'rejected'

// enum - Define named constants
enum Color {
  Red = 'RED',
  Green = 'GREEN',
  Blue = 'BLUE'
}

// Usage
const product: Product = { id: 1, name: 'Laptop', price: 999 }
const status: Status = 'pending'
const color: Color = Color.Red

🔹 Access Modifier Keywords

Control access to class members:

class BankAccount {
  // public - accessible everywhere (default)
  public accountNumber: string
  
  // private - only within class
  private balance: number
  
  // protected - within class and subclasses
  protected owner: string
  
  constructor(accountNumber: string, owner: string) {
    this.accountNumber = accountNumber
    this.balance = 0
    this.owner = owner
  }
  
  // Public method
  public deposit(amount: number): void {
    this.balance += amount
  }
  
  // Private method
  private calculateInterest(): number {
    return this.balance * 0.05
  }
}

const account = new BankAccount('123456', 'John')
account.deposit(1000)  // ✓ Works
// account.balance      // ✗ Error: private

🔹 Utility Keywords

Special keywords for type manipulation:

// readonly - Immutable properties
interface Point {
  readonly x: number
  readonly y: number
}

const point: Point = { x: 10, y: 20 }
// point.x = 30  // ✗ Error: readonly

// as - Type assertion
const value = 'hello' as string
const num = <number>someValue  // Alternative syntax

// keyof - Get keys of type
interface User {
  name: string
  age: number
}

type UserKeys = keyof User  // 'name' | 'age'

// typeof - Get type of variable
const user = { name: 'John', age: 30 }
type UserType = typeof user  // { name: string, age: number }

🔹 Generic Keywords

Create reusable, type-safe components:

// Generic function
function identity<T>(value: T): T {
  return value
}

const num = identity<number>(42)
const str = identity<string>('hello')

// Generic interface
interface Box<T> {
  content: T
}

const numberBox: Box<number> = { content: 123 }
const stringBox: Box<string> = { content: 'text' }

// Generic class
class Container<T> {
  private items: T[] = []
  
  add(item: T): void {
    this.items.push(item)
  }
  
  get(index: number): T {
    return this.items[index]
  }
}

const numbers = new Container<number>()
numbers.add(1)
numbers.add(2)

🔹 Declaration Keywords

Keywords for declaring variables and constants:

// let - Block-scoped variable
let count: number = 0
count = 10  // ✓ Can reassign

// const - Block-scoped constant
const PI: number = 3.14159
// PI = 3.14  // ✗ Error: cannot reassign

// var - Function-scoped (avoid in modern code)
var oldStyle: string = 'legacy'

// declare - Ambient declarations
declare const API_KEY: string
declare function externalFunction(): void

// namespace - Organize code
namespace Utils {
  export function log(message: string): void {
    console.log(message)
  }
}

Utils.log('Hello')

🧠 Test Your Knowledge

Which keyword makes a property immutable?