TypeORM: Property ‘id’ has no initializer and is not definitely assigned in the constructor

Last updated on February 6, 2022 A Goodman Loading... One comment

When defining entities with TypeORM and TypeScript, you may encounter an annoying issue as follows:

Property 'id' has no initializer and is not definitely assigned in the constructor

The warnings are present not only in the id field but also in all other fields in my classes entity:

The culprit here is related to strict checking of property initialization in classes. To solve the problem, we have to disable this feature. In your tsconfig.json file, find strictPropertyInitialization and set it to false:

You may have to reload or restart VS Code (or whatever code editor you are using) to make to warnings go away.

Here’s my entire tsconfig.json (with comments removed) for your reference:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "commonjs",                          
    "outDir": "./build",                              
    "rootDir": "./src",                             
    "strict": true,                                
    "moduleResolution": "node",                  
    "esModuleInterop": true,                      
    "skipLibCheck": true,                           
    "forceConsistentCasingInFileNames": true,
    "experimentalDecorators": true,
    "strictPropertyInitialization": false,
    "emitDecoratorMetadata": true
  }
}

Further reading:

You can also check out our Node.js category page for the latest tutorials and examples.

Subscribe
Notify of
guest
1 Comment
Inline Feedbacks
View all comments
baris
baris
1 month ago

This helped me thx

You May Also Like