typescript
type_error
ai_generated
true
error TS2564: Property 'X' has no initializer and is not definitely assigned in the constructor
ID: typescript/ts2564-no-initializer
95%Fix Rate
95%Confidence
50Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 5 | active | — | — | — |
Root Cause
Class property declared but not initialized. strictPropertyInitialization is enabled.
genericWorkarounds
-
95% success Initialize in the constructor or at declaration: property: Type = defaultValue
property: Type = defaultValue
Sources: https://www.typescriptlang.org/docs/handbook/2/classes.html#--strictpropertyinitialization
-
80% success Use definite assignment assertion (!) only when you're SURE it's set before use (e.g., DI)
class Foo { @Inject() service!: Service; } // OK: framework sets itSources: https://www.typescriptlang.org/docs/handbook/2/classes.html
Dead Ends
Common approaches that don't work:
-
Set strictPropertyInitialization to false
70% fail
Disables a useful safety check across the entire project
-
Add ! to suppress: property!: Type
60% fail
Suppresses the check but may cause runtime undefined access
Error Chain
Preceded by:
Frequently confused with: