Skip to content

Classes

Video Lecture

TypeScript Classes 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.

Start Script

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
interface Quux {
  quuz: string
  corge: number
}

const foo = (bar: Quux) => {
  return 'Hello, ' + bar.quuz + ' ' + bar.corge
}

const baz = { quuz: 'ABC', corge: 123 }

console.log(foo(baz))

Final Script

 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
  }

  getGarply() {
    return this.garply
  }
}

interface Quux {
  quuz: string
  corge: number
}

const baz = { quuz: 'ABC', corge: 123 }

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

console.log(fred.getGarply())

Try it,

tsc foo.ts
node foo.js