TypeScript Compiler Diagnostics

TS2564: Uninitialized class property

Strict property initialization checks whether a non-optional field is definitely assigned by construction. Here count has no initializer or constructor assignment. Initialize it on every relevant path rather than using a definite assignment assertion merely to silence the compiler. Baseline: TypeScript 5.8.3 with the shown configuration. Diagnose the reported location and nested message before applying a fix; other causes may require a different correction. The first project is intentionally invalid; create the corrected project separately. Neither example requires disabling strict checking.

Formal Syntax & Structure

typescript syntaxtypescript
TS2564 (compiler diagnostic code, not TypeScript syntax)

Working Example

Example Snippettypescript
// INTENTIONALLY INVALID — reproduce// File: tsconfig.json (omit this label){  "compilerOptions": {    "strict": true,    "target": "ES2022",    "module": "CommonJS",    "types": [],    "lib": [      "ES2022",      "DOM"    ],    "noEmitOnError": true,    "outDir": "dist"  },  "files": [    "main.ts"  ]}
// File: main.ts (omit this label)class Counter { count: number; }console.log(new Counter().count);
// Run: tsc -p . --pretty false// Expected: TS2564; no emitted main file.
// CORRECTED PROJECT// File: tsconfig.json (omit this label){  "compilerOptions": {    "strict": true,    "target": "ES2022",    "module": "CommonJS",    "types": [],    "lib": [      "ES2022",      "DOM"    ],    "noEmitOnError": true,    "outDir": "dist"  },  "files": [    "main.ts"  ]}
// File: main.ts (omit this label)class Counter { count: number; constructor() { this.count = 0; } }console.log(new Counter().count);
// Run: tsc -p . --pretty false// Expected: no diagnostics.// Run: node dist/main.js// Expected stdout: 0
TypeScript Reference