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

Updated: August 20, 2022 By: A Goodman 4 comments

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 the irritating 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
4 Comments
Inline Feedbacks
View all comments
mustakim
mustakim
9 months ago

thanks

Manuel
Manuel
1 year ago

The fix is to disable “strictPropertyInitialization” in the tsconfig.json

Wes
Wes
1 year ago

Not a fix – this check should still be performed in other regions of the code.

baris
baris
1 year ago

This helped me thx

Related Articles