TypeScript Interfaces
An interface is a declaration of a set of abstract methods—a collection of method characteristics. These methods should all be abstract and must be implemented by concrete classes; third parties can then call through this set of abstract methods, letting concrete classes execute specific methods.
A TypeScript interface is defined as follows:
interface interface_name {
}
Example
In the following example, we define an interface IPerson, then define a variable customer whose type is IPerson.
customer implements the properties and methods of the IPerson interface.
TypeScript
interface
IPerson
{
firstName
:
string
,
lastName
:
string
,
sayHi
:
(
)
=>
string
}
var
customer
:
IPerson
=
{
firstName
:
"
Tom
"
,
lastName
:
"
Hanks
"
,
sayHi
:
(
)
:
string
=>
{
return
"
Hi there
"
}
}
console
.
log
(
"
Customer object
"
)
console
.
log
(
customer
.
firstName
)
console
.
log
(
customer
.
lastName
)
console
.
log
(
customer
.
sayHi
(
)
)
var
employee
:
IPerson
=
{
firstName
:
"
Jim
"
,
lastName
:
"
Blakes
"
,
sayHi
:
(
)
:
string
=>
{
return
"
Hello!!!
"
}
}
console
.
log
(
"
Employee object
"
)
console
.
log
(
employee
.
firstName
)
console
.
log
(
employee
.
lastName
)
Note that interfaces cannot be converted to JavaScript. They are only part of TypeScript.
Compile the above code to get the following JavaScript code:
JavaScript
var
customer
=
{
firstName
:
"
Tom
"
,
lastName
:
"
Hanks
"
,
sayHi
:
function
(
)
{
return
"
Hi there
"
;
}
}
;
console
.
log
(
"
Customer object
"
)
;
console
.
log
(
customer
.
firstName
)
;
console
.
log
(
customer
.
lastName
)
;
console
.
log
(
customer
.
sayHi
(
)
)
;
var
employee
=
{
firstName
:
"
Jim
"
,
lastName
:
"
Blakes
"
,
sayHi
:
function
(
)
{
return
"
Hello!!!
"
;
}
}
;
console
.
log
(
"
Employee object
"
)
;
console
.
log
(
employee
.
firstName
)
;
console
.
log
(
employee
.
lastName
)
;
The output is:
Customer 对象
Tom
Hanks
Hi there
Employee 对象
Jim
Blakes
Union types and interfaces
The following example demonstrates how to use union types in an interface:
TypeScript
interface
RunOptions
{
program
:
string
;
commandline
:
string
[
]
|
string
|
(
(
)
=>
string
)
;
}
//
commandline is a string
var
options
:
RunOptions
=
{
program
:
"
test1
"
,
commandline
:
"
Hello
"
}
;
console
.
log
(
options
.
commandline
)
//
commandline is an array of strings
options
=
{
program
:
"
test1
"
,
commandline
:
[
"
Hello
"
,
"
World
"
]
}
;
console
.
log
(
options
.
commandline
[
0
]
)
;
console
.
log
(
options
.
commandline
[
1
]
)
;
//
commandline is a function expression
options
=
{
program
:
"
test1
"
,
commandline
:
(
)
=>
{
return
"
Hello World
"
;
}
}
;
var
fn
:
any
=
options
.
commandline
;
console
.
log
(
fn
(
)
)
;
Compile the above code to get the following JavaScript code:
JavaScript
//
commandline is a string
var
options
=
{
program
:
"
test1
"
,
commandline
:
"
Hello
"
}
;
console
.
log
(
options
.
commandline
)
;
//
commandline is an array of strings
options
=
{
program
:
"
test1
"
,
commandline
:
[
"
Hello
"
,
"
World
"
]
}
;
console
.
log
(
options
.
commandline
[
0
]
)
;
console
.
log
(
options
.
commandline
[
1
]
)
;
//
commandline is a function expression
options
=
{
program
:
"
test1
"
,
commandline
:
function
(
)
{
return
"
Hello World
"
;
}
}
;
var
fn
=
options
.
commandline
;
console
.
log
(
fn
(
)
)
;
The output is:
Hello
Hello
World
**Hello World**
Interfaces and arrays
In an interface, we can set the array's index values and elements to different types; index values can be numbers or strings.
Set the element type to string:
Example
interface
namelist
{
[
index
:
number
]
:
string
}
//
Types match, correct
var
list2
:
namelist
=
[
"
"
,
"
Runoob
"
,
"
Taobao
"
]
//
Error: element 1 is not of type string
//
var list2:namelist = ["Runoob",1,"Taobao"]
Using other types will produce an error:
Example
interface
namelist
{
[
index
:
number
]
:
string
}
//
Types match, correct
//
var list2:namelist = ["Google","Runoob","Taobao"]
//
Error: element 1 is not of type string
var
list2
:
namelist
=
[
"
John
"
,
1
,
"
Bran
"
]
After execution, the following error is reported, showing a type mismatch:
test.ts:8:30 - error TS2322: Type 'number' is not assignable to type 'string'.
8 var list2:namelist = ["John",1,"Bran"]
~
test.ts:2:4
2 [index:number]:string
~~~~~~~~~~~~~~~~~~~~~
The expected type comes from this index signature.
Found 1 error.
TypeScript
interface
ages
{
[
index
:
string
]
:
number
}
var
agelist
:
ages
;
//
Type correct
agelist
[
"
runoob
"
]
=
15
//
Type error, output error TS2322: Type '"google"' is not assignable to type 'number'.
//
agelist[2] = "google"
Interface inheritance
Interface inheritance means an interface can extend itself through other interfaces.
TypeScript allows an interface to inherit from multiple interfaces.
Inheritance uses the keyword extends.
Syntax for single-interface inheritance:
Child_interface_name extends super_interface_name
Syntax for multiple-interface inheritance:
Child_interface_name extends super_interface1_name, super_interface2_name,…,super_interfaceN_name
The inherited interfaces are separated by commas ,
Single inheritance example
TypeScript
interface
Person
{
age
:
number
}
interface
Musician
extends
Person
{
instrument
:
string
}
var
drummer
= <
Musician
{
}
;
drummer
.
age
=
27
drummer
.
instrument
=
"
Drums
"
console
.
log
(
"
Age:
"
drummer
.
age
)
console
.
log
(
"
Favorite instrument:
"
drummer
.
instrument
)
Compile the above code to get the following JavaScript code:
JavaScript
var
drummer
=
{
}
;
drummer
.
age
=
27
;
drummer
.
instrument
=
"
Drums
"
;
console
.
log
(
"
Age:
"
drummer
.
age
)
;
console
.
log
(
"
Favorite instrument:
"
drummer
.
instrument
)
;
The output is:
年龄: 27
喜欢的乐器: Drums
Multiple inheritance example
TypeScript
interface
IParent1
{
v1
:
number
}
interface
IParent2
{
v2
:
number
}
interface
Child
extends
IParent1
,
IParent2
{
}
var
Iobj
:
Child
=
{
v1
:
12
,
v2
:
23
}
console
.
log
(
"
value 1:
"
Iobj
.
v1
"
value 2:
"
Iobj
.
v2
)
Compile the above code to get the following JavaScript code:
JavaScript
var
Iobj
=
{
v1
:
12
,
v2
:
23
}
;
console
.
log
(
"
value 1:
"
Iobj
.
v1
"
value 2:
"
Iobj
.
v2
)
;
The output is:
value 1: 12 value 2: 23