Skip to main content
added 271 characters in body; added 1 character in body
Source Link
gurkan
  • 1.5k
  • 7
  • 20
  • 29

Although TypeScript analyzes the initialization of the property, if you always want to handle this case yourself, you can set to false for this setting in ts.config.json.

{ "compilerOptions": { "strict": true, "strictPropertyInitialization": false } } 

Strict Property Initialization - strictPropertyInitialization When set to true, TypeScript will raise an error when a class property was declared but not set in the constructor.

In this case, you should consider other cases too, which you will see in the links below.

class UserAccount { name: string; accountType = "user"; email: string;//Property 'email' has no initializer and is not definitely assigned in the constructor. address: string | undefined; constructor(name: string) { this.name = name; // Note that this.email is not set } } 

this.name is set specifically.
this.accountType is set by default.
this.email is not set and raises an error.
this.address is declared as potentially undefined which means it does not have to be set.

The compiler doesn't raise an error if we set the strictPropertyInitialization to false

 private _name : string; public get name() : string { return this._name; } public set name(v : string) { this._name = v; } 

https://www.typescriptlang.org/docs/handbook/2/classes.html#--strictpropertyinitialization https://www.typescriptlang.org/tsconfig#strictPropertyInitialization

Although TypeScript analyzes the initialization of the property, if you always want to handle this case yourself, you can set to false for this setting in ts.config.json.

{ "compilerOptions": { "strict": true, "strictPropertyInitialization": false } } 

Strict Property Initialization - strictPropertyInitialization When set to true, TypeScript will raise an error when a class property was declared but not set in the constructor.

In this case, you should consider other cases too, which you will see in the links below.

class UserAccount { name: string; accountType = "user"; email: string;//Property 'email' has no initializer and is not definitely assigned in the constructor. address: string | undefined; constructor(name: string) { this.name = name; // Note that this.email is not set } } 

this.name is set specifically.
this.accountType is set by default.
this.email is not set and raises an error.
this.address is declared as potentially undefined which means it does not have to be set.

https://www.typescriptlang.org/docs/handbook/2/classes.html#--strictpropertyinitialization https://www.typescriptlang.org/tsconfig#strictPropertyInitialization

Although TypeScript analyzes the initialization of the property, if you always want to handle this case yourself, you can set to false for this setting in ts.config.json.

{ "compilerOptions": { "strict": true, "strictPropertyInitialization": false } } 

Strict Property Initialization - strictPropertyInitialization When set to true, TypeScript will raise an error when a class property was declared but not set in the constructor.

In this case, you should consider other cases too, which you will see in the links below.

class UserAccount { name: string; accountType = "user"; email: string;//Property 'email' has no initializer and is not definitely assigned in the constructor. address: string | undefined; constructor(name: string) { this.name = name; // Note that this.email is not set } } 

this.name is set specifically.
this.accountType is set by default.
this.email is not set and raises an error.
this.address is declared as potentially undefined which means it does not have to be set.

The compiler doesn't raise an error if we set the strictPropertyInitialization to false

 private _name : string; public get name() : string { return this._name; } public set name(v : string) { this._name = v; } 

https://www.typescriptlang.org/docs/handbook/2/classes.html#--strictpropertyinitialization https://www.typescriptlang.org/tsconfig#strictPropertyInitialization

Source Link
gurkan
  • 1.5k
  • 7
  • 20
  • 29

Although TypeScript analyzes the initialization of the property, if you always want to handle this case yourself, you can set to false for this setting in ts.config.json.

{ "compilerOptions": { "strict": true, "strictPropertyInitialization": false } } 

Strict Property Initialization - strictPropertyInitialization When set to true, TypeScript will raise an error when a class property was declared but not set in the constructor.

In this case, you should consider other cases too, which you will see in the links below.

class UserAccount { name: string; accountType = "user"; email: string;//Property 'email' has no initializer and is not definitely assigned in the constructor. address: string | undefined; constructor(name: string) { this.name = name; // Note that this.email is not set } } 

this.name is set specifically.
this.accountType is set by default.
this.email is not set and raises an error.
this.address is declared as potentially undefined which means it does not have to be set.

https://www.typescriptlang.org/docs/handbook/2/classes.html#--strictpropertyinitialization https://www.typescriptlang.org/tsconfig#strictPropertyInitialization