October 2017
Intermediate to advanced
280 pages
6h 50m
English
TypeScript classes are similar to what ES2015 offers. However, TypeScript alters the type declarations and adds more syntax sugar. For example, let's take the Human class we defined earlier:
class Human {
static totalPeople = 0;
_name: string;
constructor(name) {
this._name = name;
Human.totalPeople += 1;
}
get name() {
return this._name;
}
set name(val) {
this._name = val;
}
talk() {
return `Hi, I'm ${this.name}!`;
}
}
There is no difference between the current TypeScript definition and the one we already introduced; however, in this case, the declaration of the _name property is mandatory. Here is how we can use the class:
let human = new Human('foo');
console.log(human._name);
Read now
Unlock full access