Classes
Classes are similar to interfaces but they’re so much more robust and powerful. In fact, classes were first introduced in ECMAScript 2015 (ES6) and can be used in-browser with a compiler like Babel. However, with TypeScript, you can use classes like in ES6, annotate their types, and compile your TypeScript code locally into ES5 for greater browser support.
Let’s take a look at a basic class
If you’ve used a more traditional programming language (like Java) before, this should look familiar.
class
Superhero
{
name
:
string
;
nickName
:
string
;
age
:
number
;
location
:
string
;
powers
:
string
[];
universe
:
string
;
constructor
(
name
:
string
,
nickName
:
string
,
age
:
number
,
location
:
string
,
powers
:
string
[])
{
this
.
name
=
name
;
this
.
nickName
=
nickName
;
this
.
age
=
age
;
this
.
powers
=
powers
;
this
.
universe
=
'Marvel'
;
}
greeting
()
{
return
`Hello, my n ame is
${
this
.
name
}
or better known as
${
this
.
nickName
}
. I am...