TypeScript Objects
An object is an instance containing a set of key-value pairs. Values can be scalars, functions, arrays, objects, etc., as in the following example:
var
object_name
=
{
key1
:
"
value1
"
,
//
Scalar
key2
:
"
value
"
,
key3
:
function
(
)
{
//
function
}
,
key4
:
[
"
content1
"
,
"
content2
"
]
//
Union
}
The above object contains scalars, functions, and collections (arrays or tuples).
Object instance
TypeScript
var
sites
=
{
site1
:
"
Runoob
"
,
site2
:
"
"
}
;
//
Accessing object values
console
.
log
(
sites
.
site1
)
console
.
log
(
sites
.
site2
)
Compile the above code to get the following JavaScript code:
JavaScript
var
sites
=
{
site1
:
"
Runoob
"
,
site2
:
"
"
}
;
//
Accessing object values
console
.
log
(
sites
.
site1
)
console
.
log
(
sites
.
site2
)
The output is:
Runoob
Google
TypeScript Type Templates
Suppose we define an object in JavaScript:
var
sites
=
{
site1
:
"
Runoob
"
,
site2
:
"
"
}
;
If we want to add a method to the object at this point, we can make the following modification:
sites.sayHello = function(){ return "hello";}
If you use the above approach in TypeScript, a compilation error will occur, because objects in TypeScript must be instances of a specific type.
TypeScript
var
sites
=
{
site1
:
"
Runoob
"
,
site2
:
"
"
,
sayHello
:
function
(
)
{
}
//
Type template
}
;
sites
.
sayHello
=
function
(
)
{
console
.
log
(
"
hello
"
sites
.
site1
)
;
}
;
sites
.
sayHello
(
)
;
Compile the above code to get the following JavaScript code:
JavaScript
var
sites
=
{
site1
:
"
Runoob
"
,
site2
:
"
"
,
sayHello
:
function
(
)
{
}
//
Type template
}
;
sites
.
sayHello
=
function
(
)
{
console
.
log
(
"
hello
"
sites
.
site1
)
;
}
;
sites
.
sayHello
(
)
;
The output is:
hello Runoob
In addition, an object can also be passed to a function as a parameter, as in the following example:
TypeScript
var
sites
=
{
site1
:
"
Runoob
"
,
site2
:
"
"
,
}
;
var
invokesites
=
function
(
obj
:
{
site1
:
string
,
site2
:
string
}
)
{
console
.
log
(
"
site1 :
"
obj
.
site1
)
console
.
log
(
"
site2 :
"
obj
.
site2
)
}
invokesites
(
sites
)
Compile the above code to get the following JavaScript code:
JavaScript
var
sites
=
{
site1
:
"
Runoob
"
,
site2
:
"
"
}
;
var
invokesites
=
function
(
obj
)
{
console
.
log
(
"
site1 :
"
obj
.
site1
)
;
console
.
log
(
"
site2 :
"
obj
.
site2
)
;
}
;
invokesites
(
sites
)
;
The output is:
site1 :Runoob
site2 :Google
Duck Typing
Duck typing is a style of dynamic typing and a form of polymorphism.
In this style, the valid semantics of an object are determined not by inheriting from a specific class or implementing a specific interface, but by the "current set of methods and properties".
It can be expressed as follows:
"When you see a bird that walks like a duck, swims like a duck, and quacks like a duck, then that bird can be called a duck."
In duck typing, the focus is on what behaviors an object can perform, not on the type to which the object belongs. For example, in languages that do not use duck typing, we can write a function that accepts an object of type "duck" and calls its "walk" and "quack" methods. In languages that use duck typing, such a function can accept an object of any type and call its "walk" and "quack" methods. If the methods that need to be called do not exist, a runtime error will be raised. The behavior in which any object with the correct "walk" and "quack" methods can be accepted by the function gave rise to the above expression, and this way of determining a type is how it got its name.
interface
IPoint
{
x
:
number
y
:
number
}
function
addPoints
(
p1
:
IPoint
,
p2
:
IPoint
)
:
IPoint
{
var
x
=
p1
.
x
p2
.
x
var
y
=
p1
.
y
p2
.
y
return
{
x
:
x
,
y
:
y
}
}
//
Correct
var
newPoint
=
addPoints
(
{
x
:
3
,
y
:
4
}
,
{
x
:
5
,
y
:
1
}
)
//
Error
var
newPoint2
=
addPoints
(
{
x
:
1
}
,
{
x
:
4
,
y
:
3
}
)