strictPropertyInitialization Guards Field Init
Each class field needs an inline initializer, a constructor assignment, or an optional marker — the strict build rejects anything that could read as undefined.
Code
class JobRunner {
// Inline initializer satisfies the check
queue: string[] = [];
// Constructor assignment satisfies the check
readonly maxRetries: number;
readonly startedAt: Date;
// Optional fields opt out: they may legitimately stay undefined
lastError?: Error;
constructor(maxRetries: number) {
this.maxRetries = maxRetries;
this.startedAt = new Date();
}
}
// Without an initializer this fails to compile under strict
// (TS2564: property 'name' has no initializer and is not
// definitely assigned in the constructor):
// class Broken {
// name: string;
// }
const runner = new JobRunner(3);
console.log('queue is empty:', runner.queue.length === 0);
console.log('retries:', runner.maxRetries);
console.log('started:', runner.startedAt instanceof Date);
console.log('last error:', runner.lastError ?? 'none');Output
Captured from a sandboxed run of this snippet in the build pipeline.
queue is empty: true
retries: 3
started: true
last error: noneThe flow of the file
strictPropertyInitialization Guards Field Init reduces to the class JobRunner.
The whole file moves through top-level bindings: runner.
Caveats
- It runs on toolchain defaults — nothing added on top.
Using it
The run prints queue is empty:, retries:, started: — each prefix names the step that produced it.
Key statements
const runner = new JobRunner(3);
this.maxRetries = maxRetries;
readonly maxRetries: number;
Interactive Tools for This Topic
FAQ
- What has to be installed first?
- Nothing — the runtime itself provides everything the code touches. Its state lives in module-level bindings: `runner`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.