static
Static fields
A static field belongs to the class itself, not to any object. Read it through the class name without calling new.
class App { static version: string = "1.0"; } console.log(App.version);
1.0
Static factories
A static method also runs without new. Factories use this to build objects with a clear name that explains the starting state.
class User { constructor(public name: string) {} static guest(): User { return new User("Guest"); } } const guest = User.guest(); console.log(guest.name);
Guest
Shared state
All objects see the same static field. Counters and caches use this to share one value across every instance.
class Ticket { static next: number = 1; id: number; constructor() { this.id = Ticket.next; Ticket.next = Ticket.next + 1; } } const first = new Ticket(); const second = new Ticket(); console.log(first.id, second.id);
1 2
this in static context
Inside a static method, this points to the class itself. Use it to read other static members so children inherit the same logic.
class Util { static prefix: string = "app"; static label(name: string): string { return this.prefix + ":" + name; } } console.log(Util.label("start"));
app:start
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a static field belong to?
Answer
The class itself
A static member lives on the class, so it is read through the class name and exists once.
-
How is a static method called?
Answer
User.guest()The method belongs to the class, not to an object, so the class name calls it.
-
Inside a static method, what does
thisname?Answer
The class itself
A static method runs without an instance, so
thispoints at the class and its static members. -
Why write a static factory instead of calling the constructor directly?
Answer
It gives the starting state a name that says what is built
A factory such as
User.guest()reads as a description of the object it returns. -
What does this program print?
class App { static version: string = "1.0"; } console.log(App.version);Answer
1.0
The field holds the string "1.0", and the class name reads it without
new. -
What does this program print?
class User { constructor(public name: string) {} static guest(): User { return new User("Guest"); } } console.log(User.guest().name);Answer
Guest
The factory returns a User whose name is the string it passed to the constructor.
-
What does this program print?
class Ticket { static next: number = 1; id: number; constructor() { this.id = Ticket.next; Ticket.next = Ticket.next + 1; } } const first = new Ticket(); const second = new Ticket(); console.log(first.id, second.id);Answer
1 2
The shared counter gives the first ticket 1 and leaves 2 for the second.
-
What does this program print?
class Base { static prefix = "base"; static label(): string { return this.prefix; } } class Child extends Base { static prefix = "child"; } console.log(Child.label());Answer
child
The inherited method runs with
thisset to Child, so it reads the child prefix. -
What does the compiler say about this program?
class App { static version: string = "1.0"; } console.log(new App().version);Answer
error TS2576: Property 'version' does not exist on type 'App'. Did you mean to access the static member 'App.version' instead?
An instance has no static member, and the compiler names the class member the call probably meant.
-
What does the compiler say about this program?
class User { constructor(public age: number) {} static describe(): string { return this.age.toString(); } } console.log(User.describe());Answer
error TS2339: Property 'age' does not exist on type 'typeof User'.
thisin a static method is the class, and an instance field such as age is not on the class.
Article author: Arthur Isaev