Static Properties and Methods
Understand Static Properties and Methods
A static member belongs to the class constructor, not to each instance.
What you will learn: the purpose of static properties and methods, the rules TypeScript checks, and what its JavaScript behavior looks like in a runnable example.
What you’ll learn
- Explain how static properties and methods works and where its type rules apply.
- Predict the exact output of the static properties and methods example.
- Diagnose a common issue: Do not expect a static counter to be isolated per instance or to persist across a process restart.
Build the mental model
Use static state for class-wide counters or utility operations, but remember that mutable static state is shared and can complicate tests. Access static members by their class name, such as Ticket.issued, rather than through an instance.
Key distinction: TypeScript checks the declared types during development; emitted JavaScript carries out the value-level operation at runtime. A type declaration alone never validates untrusted runtime input.
See the relationship
Read this illustration from the first labeled part to the last. Compare it with the example and identify which parts are type checks and which parts run in JavaScript.
Run the complete TypeScript example
This self-contained example uses fixed values, so it runs without reading input or depending on an external service. Paste it into the TypeScript Playground or save it as main.ts.
TypeScript program
class Ticket {
static issued = 0;
readonly number: number;
constructor() { this.number = ++Ticket.issued; }
static total(): number { return Ticket.issued; }
}
const a = new Ticket();
const b = new Ticket();
console.log(a.number, b.number);
console.log(Ticket.total());Verified output
1 2
2Run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. For projects with a tsconfig.json, build using that configuration instead.
Trace every important step
Ticket.issued begins at 0. Each constructor increments the shared static count and stores the new value on its own instance. The numbers are 1 and 2, while total returns 2.
Check each step: identify the input values, follow the JavaScript operation in execution order, and match the final expression with the exact output shown. An annotation can reject invalid source before execution, but does not change the value printed.
Avoid these common mistakes
Do not expect a static counter to be isolated per instance or to persist across a process restart.
How to debug: reproduce the issue in a minimal example. For compiler errors, inspect the inferred and declared types; for a runtime surprise, log the actual values and follow their references or branches.
Practice and experiment
Add Ticket.reset() as a static method and observe how it affects new ticket numbers.
Practice routine: predict the result on paper, edit one part of the code, compile under --strict, run the JavaScript, and explain whether TypeScript caught an error or JavaScript simply behaved differently.
Check your understanding
Review the key idea
Static Properties and Methods: A static member belongs to the class constructor, not to each instance.
Ticket.issued begins at 0. Each constructor increments the shared static count and stores the new value on its own instance. The numbers are 1 and 2, while total returns 2.
Before continuing, explain one valid use case, one common pitfall, and the separation between compile-time checking and runtime behavior for this feature.
Reaching the recap and completing the quizzes are separate milestones.