3 Typescript m3 Slides
3 Typescript m3 Slides
Dan Wahlin
Twitter: @danwahlin
John Papa
Twitter: @john_papa
Defining Classes
The Role of Classes in TypeScript
Fields Constructors
Properties Functions
Defining a Class
class
Car
{
//Fields
//Constructor
Classes act as containers
that encapsulate code
//Properties
//Functions
}
Defining Constructors
class
Car
{
engine:
string;
Field
constructor(engine:
string)
{
Constructor
this.engine
=
engine;
}
}
Shorthand way to
declare a field
class
Car
{
constructor(public
engine:
string)
{
}
}
Adding Functions
class
Car
{
engine:
string;
Class members are
public by default
constructor
(engine:
string)
{
this.engine
=
engine;
}
start()
{
return
"Started
"
+
this.engine;
}
stop()
{
return
"Stopped
"
+
this.engine;
}
}
Defining Properties
class
Car
{
private
_engine:
string;
constructor(engine:
string)
{
this.engine
=
engine;
}
get
engine():
string
{
return
this._engine;
Properties act as filters and
}
can have get or set blocks
set
engine(value:
string)
{
if
(value
==
undefined)
throw
'Supply
an
Engine!';
this._engine
=
value;
}
}
Using Complex Types
class
Engine
{
constructor(public
horsePower:
number,
public
engineType:
string)
{
}
}
Complex Type
class
Car
{
private
_engine:
Engine;
constructor(engine:
Engine)
{
this.engine
=
engine;
}
...
}
Instantiating a Type
This fails
This succeeds
https://github.com/borisyankov/DefinitelyTyped
http://definitelytyped.org/
Extending Types
Extending Types with TypeScript
Auto
extends extends
Truck SUV
Extending a Type
class
Auto
{
engine:
Engine;
constructor(engine:
Engine)
{
this.engine
=
engine;
}
}
Truck derives from
Auto
class
Truck
extends
Auto
{
fourByFour:
boolean;
constructor(engine:
Engine,
fourByFour:
boolean)
{
super(engine);
Call base class
constructor
this.fourByFour
=
fourByFour;
}
}
Using Interfaces
What's an Interface?
Engine 1
Start Stop
Start
Engine 2
Engine 3
Defining an Interface
interface
IEngine
{
start(callback:
(startStatus:
boolean,
engineType:
string)
=>
void)
:
void;
stop(callback:
(stopStatus:
boolean,
engineType:
string)
=>
void)
:
void;
}
IEngine Interface
defines 2 members
Understanding Functions in an Interface
interface
IEngine
{
start() accepts a single start() doesn't
parameter named callback return any data
start(callback:
(startStatus:
boolean,
engineType:
string)
=>
void)
:
void;
} callback() doesn't
callback parameter must be a return any data
function that accepts a
boolean and a string as
parameters
Optional Members in an Interface
interface
IAutoOptions
{
engine:
IEngine;
basePrice:
number;
state:
string;
make?:
string;
Optional Members
model?:
string;
year?:
number;
}
Implementing an Interface
class
Auto
{
engine:
IEngine;
basePrice:
number;
//More
fields
constructor(data:
IAutoOptions)
{
this.engine
=
data.engine;
this.basePrice
=
data.basePrice;
}
}
Extending an Interface
Extending an Interface
interface
IAutoOptions
{
engine:
IEngine;
basePrice:
number;
state:
string;
make?:
string;
model?:
string;
year?:
number;
}
Defines IAutoOptions members plus
custom members
interface
ITruckOptions
extends
IAutoOptions
{
bedLength?:
string;
fourByFour:
boolean;
}
Using an Extended Interface