Constructors

constructor()

The constructor runs once when an object is created with new. Use it to store the starting values of fields.

Assign every declared field that has no initial value inside the constructor, so no field stays empty by accident.

class User { name: string; constructor(name: string) { this.name = name; } } const user = new User("Ada"); console.log(user.name);

Ada

Parameter properties

A modifier on a constructor parameter declares and assigns a field in one step. It removes the manual field declaration and the manual assignment.

class User { constructor(public name: string, private age: number = 0) {} } const user = new User("Ada", 36); console.log(user.name);

Ada

Initialize safely

Combine default values with optional parameters for flexible construction. Keep the fallback logic in one place so every object starts complete.

class Config { host: string = "localhost"; port: number = 8080; constructor(host?: string) { if (host !== undefined) { this.host = host; } } } const local = new Config(); const remote = new Config("example.com"); console.log(local.host, local.port); console.log(remote.host, remote.port);

localhost 8080

example.com 8080

Require with strictPropertyInitialization

With strict mode on, every field must get a value from an initializer or from the constructor. The compiler stops the build when a field could stay empty.

The fix below is the common pattern: initialize the field where it is declared.

class User { name: string = "Guest"; } const user = new User(); console.log(user.name);

Guest

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. When does a constructor run?

    Answer

    Once, when an object is created with new

    The constructor is part of the creation of each instance and runs before the caller gets it.

  2. What does constructor(public name: string) do that a plain parameter does not?

    Answer

    It declares and assigns the field name in one step

    A modifier on a constructor parameter creates the field and assigns the argument to it.

  3. What does strict property initialization require?

    Answer

    Every field gets a value from an initializer or from the constructor

    A field that could stay empty stops the build, which is why the usual fix is an initial value.

  4. What does this program print? class User { name: string; constructor(name: string) { this.name = name; } } const user = new User("Ada"); console.log(user.name);

    Answer

    Ada

    The constructor assigns the argument to the field, so the object starts complete.

  5. What does this program print? class User { constructor(public name: string, private age: number = 0) {} } const user = new User("Ada", 36); console.log(user.name);

    Answer

    Ada

    The public parameter property is readable outside the class, and it holds the first argument.

  6. What does this program print? class Config { host: string = "localhost"; port: number = 8080; constructor(host?: string) { if (host !== undefined) { this.host = host; } } } const local = new Config(); console.log(local.host, local.port);

    Answer

    localhost 8080

    The missing argument keeps both initial values, so the object prints them as they were declared.

  7. What does this program print? class Config { host: string = "localhost"; port: number = 8080; constructor(host?: string) { if (host !== undefined) { this.host = host; } } } const remote = new Config("example.com"); console.log(remote.host, remote.port);

    Answer

    example.com 8080

    The argument replaces the host, while the port keeps its default.

  8. What does the compiler say about this program? class User { name: string; } const user = new User(); console.log(user.name);

    Answer

    error TS2564: Property 'name' has no initializer and is not definitely assigned in the constructor.

    Strict property initialization refuses a field that neither an initializer nor the constructor fills.

  9. What does the compiler say about this program? class User { constructor(public name: string, private age: number) {} } const user = new User("Ada", 36); console.log(user.age);

    Answer

    error TS2341: Property 'age' is private and only accessible within class 'User'.

    The private parameter property is a field of the class only, so reading it outside is a type error.

  10. What does the compiler say about this program? class User { constructor(public name: string) {} } console.log(new User().name);

    Answer

    error TS2554: Expected 1 arguments, but got 0.

    The constructor needs the name, and the call creates the object without it.

Article author: Arthur Isaev