C.W.K.
Stream
Lesson 04 of 06 · published

`static` Members and `this` in Classes

~8 min · classes, static, this

Level 0Curious
0 XP0/93 lessons0/23 achievements
0/100 XP to next level100 XP to go0% complete
"Static members live on the class, not on instances. this inside a static method is the class itself."

Static members

static declares a member that belongs to the class, not to each instance. class Foo { static count = 0 } means Foo.count is one value shared across all uses; new Foo().count doesn't exist. Static fields and methods are accessed via the class name, not via instances.

Common uses: factory methods (static create()), counters or registries (static instances: Foo[] = []), and constants (static readonly MAX = 100).

this inside static methods

Inside a static method, this refers to the class itself (not an instance). static create(): Foo { return new this() } creates a new instance via this — and this here is the class. This pattern works correctly for inheritance: a subclass's create calls the subclass's constructor, not the parent's, because this is bound to the calling class.

The this type

TypeScript supports the polymorphic this type as a return annotation. class Builder { setX(): this } says "return whatever this is at the call site" — usually the calling class, allowing fluent chaining where each method returns the most-derived type. builder.setX().setY() works through subclasses too.

static is for class-level state; this type is for instance-level fluent APIs. Together they cover most cases where you want operations that work across an entire inheritance hierarchy.

Code

Static members and polymorphic this·typescript
class Counter {
  static instances = 0;
  static readonly MAX = 100;

  // Factory method using `new this()`.
  static create(): Counter {
    Counter.instances++;
    return new this();
  }

  // Polymorphic `this` for fluent chaining.
  reset(): this {
    return this;
  }

  count: number = 0;
}

class SafeCounter extends Counter {
  // Inherits static instances and MAX.
  // create() automatically returns SafeCounter, not Counter, via `new this()`.
}

const c = Counter.create();              // c: Counter
const s = SafeCounter.create();          // s: SafeCounter — same factory pattern
Counter.instances;                       // 2

c.reset().reset();                       // ✅ fluent — each returns `this`

External links

Exercise

Build a Logger class with static level: 'info' | 'error' = 'info', a constructor that takes a prefix, and a log(msg: string): void method. Add a static setLevel(l: ...). Confirm Logger.level is shared across all instances.
Hint
Each instance reads Logger.level for its current setting. Calling Logger.setLevel('error') once affects every existing and future logger — that's the point of static.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.