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

avatar

Borislav Hadzhiev

Fri Mar 18 20223 min read

banner

Photo by Ryo Yoshitake

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

The error "Property has no initializer and is not definitely assigned in the constructor" occurs when we declare a class property without initializing it. To solve the error, provide an initial value for the class property, e.g. name: string = 'James'; or use a non-null assertion.

property has no initializer

Here is an example of how the error occurs.

index.ts
class Employee { // ⛔️ Error: Property 'name' has no initializer // and is not definitely assigned in the constructor.ts(2564) name: string; salary: number; tasks: string[]; }

We have declared properties of specific type on the class, but have not given them initial values.

The cause of the error is - we have declared a name property of type string, but the name property in our class does not have a value of type string, it is actually undefined.

One way to solve the error, is to provide initial values for the class properties.

index.ts
class Employee { name = 'James Doe'; salary = 0; tasks: string[] = []; address: { country: string; city: string } = { country: '', city: '', }; }

If you don't want to provide initial values for the fields and want to get rid of the error, you can use the non-null assertion operator (!).

index.ts
class Employee { name!: string; salary!: number; tasks!: string[]; }

The non-null assertion operator (!) removes null and undefined from a type without doing any explicit type checking.

We effectively tell TypeScript that the class properties are of the specified type and are not null or undefined.

An alternative approach is to provide initial values for your class properties inside of the classes' constructor method.

index.ts
class Employee { name: string; salary: number; tasks: string[]; constructor() { this.name = ''; this.salary = 0; this.tasks = []; } }

This is very similar to what we did in the first code snippet.

Another way to solve the "Property has no initializer" error is to mark the class properties as optional.

index.ts
class Employee { name?: string; salary?: number; tasks?: string[]; }
We have marked the class properties as optional. In other words they can either be the specified type or be undefined.

Now we are able to declare the properties, without giving them a value, because undefined is included in their type.

If you want to disable type checking for property initialization for your entire project, you can do that with the strictPropertyInitialization option in your tsconfig.json file.

tsconfig.json
{ "compilerOptions": { // ... rest "strictPropertyInitialization": false } }

If your tsconfig.json file sets the strict option to true, make sure to add strictPropertyInitialization below strict.

tsconfig.json
{ "compilerOptions": { "strict": true, // 👈️ if you have this "strictPropertyInitialization": false // 👈️ add this below } }

Now you are able to declare class properties without initializing them:

index.ts
class Employee { name: string; salary: number; tasks: string[]; }

If this doesn't take effect, try restarting your IDE.

When the strictPropertyInitialization option is set to true, the type checker throws an error when we declare a class property, but don't provide an initial value for it, either inline or in the constructor.

Conversely, if set to false, no errors are thrown when class properties without initializer are declared.

If you set strictPropertyInitialization to false, it applies to your entire project, which might not be what you want.

All in all, the "Property has no initializer" error is caused when we declare a class property of specific type, without giving it a value of the specified type.

The best solution for the error is to either provide an initial value for the property, or mark the property as optional.

Use the search field on my Home Page to filter through my more than 1,000 articles.