Below is an example of creating a TypeScript’s password verifier with the help of the get and the set property accessors of TypeScript. This program will allow the user to set the password and then get the password again to verify it before printing the outcome accordingly. The property accessors look like a property of a class from the outside but actually they are simply functions within that class.
interface IComputer { brand : string | undefined; user? : string | undefined; year? : number | undefined; showBrand(password : string) : void; } class Computer implements IComputer { public brand : string; public user : string; public year : number; private password : string = "seriously"; constructor(brand : string, user : string, year : number ) { this.brand = brand this.user = user this.year = year } showBrand(password : string) : void { if(password == this.password) { console.log(`The brand of this pc is ${this.brand} and the name of this pc is ${this.user} and its year is ${this.year}`) } else { console.log("The password does not match!") } } set reset(password : string) { this.password = password } get reset() : string { return this.password } } let computer = new Computer("Dell", "localhost", 2022); computer.reset = "aeiou" computer.showBrand("pick") computer.showBrand(computer.reset)
As you can see, the user can then change the initial password and if the user entered the wrong password then the program will output the message stating that the password does not match at all!
The password does not match! The brand of this pc is Dell and the name of this pc is localhost and its year is 2022
The second line of the outcomes shows the user has entered the exact password using the get’s property accessor.