Introduction to Interfaces
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 39 of 73.
Interfaces serve the same fundamental purpose as type aliases for objects but use a different syntax and offer some unique capabilities.
An interface is declared using the interface keyword followed by the interface name and the object structure in curly braces:
interface Animal {
name: string;
sound: string;
}
let dog: Animal = {
name: "Buddy",
sound: "Woof"
};The syntax is clean and straightforward - you simply list the properties and their types within the interface body. Once defined, you can use the interface name as a type annotation anywhere in your code, just like with type aliases.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyCreate an interface named Pet with the following properties:
nameof typestringspeciesof typestringageof typenumberisVaccinatedof typeboolean
Create an interface named Vehicle with the following properties:
makeof typestringmodelof typestringyearof typenumber
Using your interfaces, create the following variables:
- Create a variable named
myDogof typePetwith name"Buddy", species"Golden Retriever", age3, and isVaccinatedtrue - Create a variable named
myCatof typePetwith name"Whiskers", species"Persian", age2, and isVaccinatedfalse - Create a variable named
myCarof typeVehiclewith make"Toyota", model"Camry", and year2022
Create a function named describePet that accepts a parameter of type Pet and returns a string in the format "[name] is a [age]-year-old [species]".
Create a function named getVehicleInfo that accepts a parameter of type Vehicle and returns a string in the format "[year] [make] [model]".
Print the following outputs on separate lines:
- Call
describePetwithmyDogand print the result - Call
describePetwithmyCatand print the result - Call
getVehicleInfowithmyCarand print the result - Print the vaccination status of
myDog(theisVaccinatedproperty)
Cheat sheet
Interfaces define object structure using the interface keyword:
interface Animal {
name: string;
sound: string;
}Use interfaces as type annotations for variables:
let dog: Animal = {
name: "Buddy",
sound: "Woof"
};Try it yourself
// TODO: Write your code here
// Create the Pet interface
// Create the Vehicle interface
// Create the variables using your interfaces
// Create the describePet function
// Create the getVehicleInfo function
// Print the required outputsThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction To TypeScript
1Getting Started with TS
What is TypeScript?Why Use TypeScript?Your First TypeScript CodeCompilation Process & ErrorsRecap: Introduction to TS4Working with Functions
Typing Params & Return ValuesTyping Arrow FunctionsThe 'void' Return TypeOptional Parameters with '?'Default Parameter ValuesTyping Rest ParametersDefining Function TypesRecap: Building Typed Funcs2Core Types
Basic Types: str, num, booleanThe 'any' Type: Escape HatchThe 'unknown' TypeWorking with 'null' & 'undef'Type Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Types: Aliases, Unions & Inter
Type Aliases for PrimitivesUnion Types ('|')Working with Union TypesLiteral TypesIntersection Types ('&')Combining Type AliasesRecap: Advanced Type Combos3Data Structure: Arrays & Tuple
Typed Arrays'readonly' Modifier for ArraysWhat is a Tuple?Declaring and Accessing TuplesDestructuring TuplesReadonly TuplesMulti-dimensional Typed Arrays Spread Operator with ArraysRecap: Arrays and Tuples6Typing Objects and Interfaces
Inline Object Type AnnotationsType Aliases for ObjectsIntroduction to InterfacesInterfaces vs. Type AliasesOptional & Readonly PropsExtending Interfaces and TypesAdding Methods to InterfacesRecap: Defining Object Shapes