Skip to content

Instantly share code, notes, and snippets.

@arnoldc
Last active November 3, 2021 10:45
Show Gist options
  • Select an option

  • Save arnoldc/41cd44e86f5fced37ce2b4e38115722c to your computer and use it in GitHub Desktop.

Select an option

Save arnoldc/41cd44e86f5fced37ce2b4e38115722c to your computer and use it in GitHub Desktop.
[Typescript] Access modifiers
// make sure access modifier are stated (e.g public , private)
// adding readonly for not needed to be modified
export class User {
public readonly id: number;
public firstName: string;
public lastName: string;
public email: string;
protected dob: Date;
public get fullName(): string {
return `${this.firstName} ${this.lastName}`;
}
public doesEmailMatch(email: string): boolean {
return this.email === email;
}
}
// creating private method accesible in the class
export class Admin extends User {
public readonly yearBorn: number;
constructor(firstName: string, lastName: string, email: string) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.yearBorn = this.getYear();
}
private getYear(): number {
return this.dob.getFullYear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment