AI Hints
Home›TypeScript Tutorial›More About Classes
TypeScript · Lesson 49 of 120

Static Members

On the class.

⏱ 2 min read▶ Video at 38:44🎓 Free
Watch this lesson in the TypeScript courseWatch on YouTube ↗Full TypeScript course

Most properties belong to each object. A static member belongs to the class itself, shared by all. Like a counter of how many orders have been created.

Static Members
From the video: Static Members
  • Per object
  • Or per class
  • static

Numbering orders

Next ID is static, shared by every order. Each new order takes the next number. We read it through the class name. Run it.

app.tstypescript
class Order {
  static nextId = 1042;
  readonly id: number;

  constructor(public total: number) {
    this.id = Order.nextId++;
  }
}

const a = new Order(65);
const b = new Order(30);
console.log(a.id, b.id, "| next:", Order.nextId);
Output
1042 1043 | next: 1044

Summary

Static members belong to the class, shared by every object. Next, getters and setters.

Key takeaways
  • static nextId
  • Order.nextId
  • Shared
Next up: Getters and Setters — computed properties