Skip to content

Instantly share code, notes, and snippets.

@jerkovicl
Last active April 23, 2019 11:04
Show Gist options
  • Save jerkovicl/387e2f1c64d649f010756a11ca40fc71 to your computer and use it in GitHub Desktop.
Save jerkovicl/387e2f1c64d649f010756a11ca40fc71 to your computer and use it in GitHub Desktop.
how to use interfaces and classes in Angular
  • if you need some methods in your interfaces for some custom logic
interface IPerson {
    firstName: string;
    lastName: string;
    age: number;
    getFullName(): string;
}
class Person implements IPerson {
    public firstName: string;
    public lastName: string;
    public age: number;
    constructor(firstName: string, lastName: string, age: number) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }
    getFullName(): string {
        return `${this.firstName} ${this.lastName}`
    }
}

const person: IPerson = new Person('John', 'Doe', 44);
  • if you only need typechecking
interface IPerson {
    firstName: string;
    lastName: string;
    age: number;
}

const person: IPerson = <IPerson>{
    firstName: 'John',
    lastName: 'Doe',
    age: 44
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment