Table of Contents

Typescript - Class

About

Classes in TypeScript are just a shorthand for the prototype-based OO

public is a shorthand that creates properties from the argument with the same name.

Example

class Student {
    fullName: string;
    constructor(
        public firstName: string, 
        public middleInitial: string, 
        public lastName: string
     ) {
        this.fullName = firstName + " " + middleInitial + " " + lastName;
    }
}
interface Person {
    firstName: string;
    lastName: string;
}
let user = new Student("Jane", "M.", "User");
function greeter(person: Person) {
    return "Hello, " + person.firstName + " " + person.lastName;
}
document.body.textContent = greeter(user);