Sub topic 9
strictPropertyInitialization in Classes
This chapter assumes you know TypeScript classes, constructors, and type annotations. It also assumes that "strict": true is enabled in tsconfig.json. The strictPropertyInitialization option checks that every required instance property receives a value before the constructor finishes.
Why initialization matters
A class property is an instance value stored on each object created from the class. If TypeScript declares a property as a string, callers expect it to contain a string whenever the object exists.
Without strict property initialization, this class compiles even though name is never assigned:
class User {
name: string;
}
const user = new User();
console.log(user.name.toUpperCase());At runtime, user.name is undefined, so calling toUpperCase() throws an error. With strictPropertyInitialization, TypeScript reports that the property has no initializer and is not definitely assigned in the constructor.
The usual fix is to assign the property in the constructor:
class User {
name: string;
email?: string;
constructor(name: string, email?: string) {
this.name = name;
this.email = email;
}
}
const user = new User("Ada Lovelace");
console.log(user.name.toUpperCase());The email property is optional because its type includes undefined through the ? marker. It does not need to be assigned.
How the check works
TypeScript follows the assignments that can happen during construction. Every required property must be initialized either at its declaration or along every constructor path.
class Connection {
readonly host = "localhost";
readonly port: number;
constructor(useTls: boolean) {
if (useTls) {
this.port = 443;
} else {
this.port = 80;
}
}
}Here, port is assigned in both branches. The property is also readonly, which means it can be assigned during construction but not changed afterward.
A property initializer is useful when every instance should start with the same default:
class RetryPolicy {
maxAttempts = 3;
delayMs = 500;
}Common errors and deliberate exceptions
A frequent error occurs when assignment happens only conditionally:
class Account {
name: string;
constructor(name?: string) {
if (name !== undefined) {
this.name = name;
}
}
}The constructor can finish without assigning name. Require the value instead, provide a default, or make the property optional:
class Account {
name: string;
constructor(name = "Guest") {
this.name = name;
}
}The definite-assignment assertion, written as !, tells the compiler that another part of the program initializes the property:
class Service {
client!: { request(): void };
start(client: { request(): void }): void {
this.client = client;
}
}Use ! sparingly. It removes the compiler's protection, so reading client before calling start can still fail at runtime.
Exercise
Why does email?: string not require an assignment, while name: string does?
Show answer
The question mark makes email optional, so its value may be undefined. name must always contain a string, so the constructor or a property initializer must assign it.