Interfaces
What are interfaces? There are no “interfaces” in JavaScript. That is correct. Interfaces a TypeScript-specific concept that exists only in the TypeScript project. We can prove this by creating an interface within the TypeScript playground; it will not compile to JavaScript.
Interfaces are essentially objects with their properties annotated. Once an interface is created, that object becomes a data type that you can check against.
Let’s look at a traditional JavaScript object and a TypeScript interface.
index.js
const
person
=
{
name
:
''
,
nickName
:
''
,
location
:
''
,
age
:
0
,
}
index.ts
interface
Person
{
name
:
string
;
nickName
:
string
;
location
:
string
;
age
:
number
;
}
We can now assign a const
with the type of Person
and provide values.
const
person
:
Person
=
{
name
:
'Peter Parker'
,
nickName
:
'Spider-Man'
,
location
:
'New York, NY'
,
age
:
27
}
console
.
log
(
person
);
If you...