Course contentsShow
← TypeScript
Lesson 1056 of 1,79624. Classes in TypeScriptFree lesson

Static Properties Basics

Learn how to define static properties on classes that belong to the class itself rather than instances.

Static Properties Basics

What you'll learn: How to define properties that belong to the class itself rather than to individual instances.

The Core Concept

In TypeScript (and JavaScript), static properties are properties that exist on the class constructor itself, not on objects created from that class. Think of them as "class-level" data that's shared across all instances, or data that relates to the class as a concept rather than to any particular object.

The Analogy

Imagine a factory that makes cars. Each car (instance) has its own color, mileage, and VIN number — those are instance properties. But the factory itself has a single address, a company motto, and a total count of cars ever produced. Those factory-level attributes are like static properties: they belong to the factory (the class), not to any individual car (instance).

The Syntax

You define static properties using the static keyword before the property name:

class Database {
  static maxConnections: number = 100;
  static activeConnections: number = 0;

  connectionId: number;

  constructor() {
    this.connectionId = ++Database.activeConnections;
  }
}

// Access static properties via the class name
console.log(Database.maxConnections); // 100

// Create instances
const db1 = new Database();
const db2 = new Database();

console.log(Database.activeConnections); // 2
console.log(db1.connectionId); // 1
console.log(db2.connectionId); // 2

Notice how maxConnections and activeConnections are accessed through Database.propertyName, not through instances like db1 or db2.

Key Characteristics

Class ownership: Static properties live on the class constructor function itself. You access them using the class name, not an instance variable.

Shared state: All code accessing a static property sees the same value. There's only one copy, regardless of how many instances exist.

Type safety: Static properties are fully type-checked just like instance properties.

When to Use Static Properties

  • Configuration constants (like maxConnections above)
  • Counters or caches shared across all instances
  • Default values or fallback settings
  • Registry patterns where the class tracks all instances

Key Takeaway: Static properties are typed, class-level data accessed via the class name itself — they're shared across all instances and exist independently of any particular object.