Skip to content

Classes

Video Lecture

TypeScript Classes

Description

A Class is essentially a blueprint of what an object is supposed to look like when implemented. A Class can have initialized properties and methods to help create and modify the objects.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Grault {
    private garply: string

    constructor(quux: Quux, waldo: number[]) { 
        this.garply = quux.quuz + " " + quux.corge + " " + waldo
    }

    public getGarply(){
        return this.garply
    }
}

interface Quux {
    quuz: string;
    corge: number;
}

let baz = { quuz: "ABC", corge: 123 };

let fred: Grault = new Grault(baz, [1,2,3])

console.log(fred.getGarply());

Try it,

1
2
tsc foo.ts
node foo.js