How to Use Static Members in TypeScript
Use static members in TypeScript when data or behavior belongs to the class itself instead of individual instances. This is perfect for utility helpers, shared counters, constants, factory methods, and configuration values.
What you’ll build or solve
You’ll learn how to use static properties and methods in TypeScript classes. You’ll also know when static members are better than instance members.
Learn TypeScript on Mimo
When this approach works best
This approach is the right choice when the logic should be shared globally across all instances.
Common real-world scenarios include:
- App constants
- ID counters
- Factory helpers
- Validation utilities
- Shared configuration
This is a bad idea when each object instance needs its own separate state.
Prerequisites
You only need:
- Basic TypeScript classes
- Properties and methods
- Understanding of instances vs classes
Step-by-step instructions
Step 1: Create a static property
Static properties live on the class.
class AppConfig {
static version: string = "1.0.0";
}
Access it directly from the class.
console.log(AppConfig.version);
No instance is required.
Step 2: Create a static method
Static methods are ideal for helpers.
class MathHelper {
static double(value: number): number {
return value * 2;
}
}
Call it from the class.
MathHelper.double(5);
This keeps utility logic grouped and discoverable.
Step 3: Use static counters
A common real-world pattern is shared counting.
class User {
static totalUsers = 0;
constructor() {
User.totalUsers++;
}
}
This tracks global instance creation.
What to look for:
- Static members belong to the class
- Access them without
new - Great for helpers and constants
- Useful for counters and factories
- Do not use
thisfrom instances
Examples you can copy
App version
static version = "2.0";
Utility helper
static formatEmail(email: string): string
Shared counter
static totalOrders = 0;
Common mistakes and how to fix them
Mistake 1: Accessing static members from an instance
What the reader might do:
const user = new User();
user.totalUsers;
Why it breaks: static members exist on the class, not the instance.
Corrected approach:
Use User.totalUsers.
Mistake 2: Using static for per-instance state
What the reader might do:
Store each user’s name as static.
Why it breaks: all instances would share the same value.
Corrected approach:
Use normal instance properties.
Mistake 3: Overusing static helpers instead of modules
What the reader might do:
Create large utility classes full of unrelated static methods.
Why it breaks: the class becomes a dumping ground.
Corrected approach:
Use focused modules when class grouping adds no value.
Troubleshooting
If the member is undefined, access it through the class.
If values leak across instances, remove static.
If helpers feel unrelated, move them into utility modules.
If counters drift, inspect constructor execution paths.
Quick recap
- Static members belong to the class
- Access them without instances
- Great for helpers and constants
- Use static counters for shared tracking
- Avoid static for per-instance data
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot