Search This Blog

2023/09/10

Typescript : strictPropertyInitialization flag in tsconfig

 In tsconfig.json there is flag strictPropertyInitialization




Here is example code


class UserAccount {
name: string;
accountType = "user";
email: string;
address: string | undefined;

constructor(name: string) {
this.name = name;
// Note that this.email is not set
}
}

In this class in constructor we are not initializing variable email which is not initilized at time of declaration also.
This will give error.

To Fix it



class UserAccount {
name: string;
accountType = "user";
email: string;
address: string | undefined;

constructor(name: string,email:string) {
this.name = name;
this.email = email
}
}

No comments:

Post a Comment