Class Constructor in TypeScript

Veröffentlicht am: 28 Dezember 2025
auf dem Kanal: Learn Language Hub
7
0

In TypeScript, a class constructor is a special method used to initialize an object when a class is instantiated.

It’s very similar to JavaScript, but TypeScript adds strong typing, access modifiers, and shortcuts.

1. Basic constructor
class User {
name: string;
age: number;

constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
}

const user = new User("Alice", 30);

What’s happening

constructor runs automatically when new User(...) is called

It assigns initial values to class properties

Parameters are typed

2. Constructor parameters are typed

TypeScript enforces correct types:

new User("Alice", 30); // ✅
new User(30, "Alice"); // ❌ Type error


This prevents runtime bugs at compile time.

3. Parameter properties (TypeScript shortcut)

TypeScript provides a shorthand to declare and initialize properties directly in the constructor.

class User {
constructor(
public name: string,
private age: number
) {}
}


This is equivalent to:

class User {
public name: string;
private age: number;

constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
}


✅ Very common in real-world TS
✅ Less boilerplate

4. Access modifiers in constructors

You can use access modifiers in constructor parameters:

public → accessible everywhere

private → only inside the class

protected → class + subclasses

readonly → cannot be changed after initialization

class Product {
constructor(
public name: string,
readonly price: number
) {}
}

const p = new Product("Book", 20);
p.price = 25; // ❌ Error (readonly)

5. Optional constructor parameters

Use ? or default values.

Optional parameter
class User {
constructor(public name: string, public age?: number) {}
}

Default value
class User {
constructor(public name: string, public age: number = 18) {}
}

6. Multiple constructors? (Not directly)

TypeScript does not support multiple constructors, but you can simulate them using optional parameters or union types.

class Point {
x: number;
y: number;

constructor(x: number, y?: number) {
this.x = x;
this.y = y ?? 0;
}
}

7. Constructor with inheritance (super)

When extending a class, you must call super() before using this.

class Animal {
constructor(public name: string) {}
}

class Dog extends Animal {
constructor(name: string, public breed: string) {
super(name); // required
}
}


❌ Using this before super() causes an error.

8. Constructors and interfaces

Constructors themselves cannot be defined in interfaces, but the shape of the instance can be.

interface UserShape {
name: string;
}

class User implements UserShape {
constructor(public name: string) {}
}

9. When constructors run
class Logger {
constructor() {
console.log("Instance created");
}
}

new Logger(); // logs immediately


Constructors run once per instance.

10. Common best practices

✅ Use parameter properties to reduce boilerplate
✅ Keep constructors lightweight (no heavy logic)
✅ Validate inputs if needed
❌ Avoid async constructors (not supported)

Quick summary

constructor initializes a class instance

Parameters are strongly typed

Access modifiers can be declared in constructor params

readonly prevents reassignment

super() is required in subclasses

Only one constructor per class


Auf dieser Seite können Sie das Online-Video Class Constructor in TypeScript mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Learn Language Hub 28 Dezember 2025 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 7 Mal angesehen und es wurde von 0 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!