Extending Interfaces and Types
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 42 of 73.
Instead of duplicating code, TypeScript provides powerful mechanisms to build new types based on existing ones through inheritance.
For interfaces, you can use the extends keyword to create a new interface that inherits all properties from a base interface:
interface Vehicle {
brand: string;
year: number;
}
interface Car extends Vehicle {
doors: number;
}
// Car now has: brand, year, and doorsFor type aliases, you achieve the same result using intersection types with the & operator:
type Vehicle = {
brand: string;
year: number;
};
type Car = Vehicle & {
doors: number;
};
// Car has the same properties as the interface exampleBoth approaches create new types that contain all the properties from the base type plus any additional properties you define. This promotes code reusability and helps maintain consistency across related types in your application.
Challenge
EasyCreate a base interface named Employee with the following properties:
idof typenumbernameof typestringdepartmentof typestring
Create an interface named Manager that extends Employee and adds the following properties:
teamSizeof typenumberbudgetof typenumber
Create a type alias named Contact with the following properties:
emailof typestringphoneof typestring
Create a type alias named Developer using intersection types that combines Employee and Contact, and adds the following properties:
programmingLanguagesof typestring[]yearsExperienceof typenumber
Using your interfaces and types, create the following variables:
- Create a variable named
teamLeadof typeManagerwith id101, name"Alice Johnson", department"Engineering", teamSize8, and budget250000 - Create a variable named
softwareDevof typeDeveloperwith id102, name"Bob Smith", department"Engineering", email"bob.smith@company.com", phone"555-0123", programmingLanguages["TypeScript", "Python", "Java"], and yearsExperience5
Create a function named getManagerSummary that accepts a parameter of type Manager and returns a string in the format "[name] manages [teamSize] people with a budget of $[budget]".
Create a function named getDeveloperSkills that accepts a parameter of type Developer and returns a string in the format "[name] knows [programmingLanguages joined with ', '] ([yearsExperience] years experience)".
Print the following outputs on separate lines:
- Call
getManagerSummarywithteamLeadand print the result - Call
getDeveloperSkillswithsoftwareDevand print the result - Print the department of
teamLead - Print the email of
softwareDev
Try it yourself
// TODO: Write your code here
// Create the Employee interface
// Create the Manager interface that extends Employee
// Create the Contact type alias
// Create the Developer type alias using intersection types
// Create the teamLead variable of type Manager
// Create the softwareDev variable of type Developer
// Create the getManagerSummary function
// Create the getDeveloperSkills 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