EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript Extend Type
 

TypeScript Extend Type

Lalita Gupta
Article byLalita Gupta
EDUCBA
Reviewed byRavi Rathore

Updated February 17, 2023

TypeScript Extend Type

 

 

Definition of TypeScript Extend Type

TypeScript extend type is defined as the typescript interfaces that can extend classes which means that the interfaces can take over the members of a class but without an execution in which the class behaves as an interface that can have all the members which can be communicated without execution, ‘extends’ keyword has been used to extend the types in the typescript and also for generating the UserActivity interfaces can extend the event type. We can extend it by appending the string, which is the ‘UserId’ field within the UserActivity, and can also take over the remaining from the event.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Introduction to TypeScript Extend Type

Typescript is the language that can manage various types of values. It can give data type for JavaScript to convert it into the strongly typed programming language, for which JavaScript does not manage with the data type, as it has an extended type that can be utilized for extending a type into the typescript with the help of the ‘extend’ keyword. It can be extended with the class by implementing extending the interfaces by classes as well as with the interfaces in which interfaces can extend with each other same as in classes in which we can also say that it is flexible in using the interfaces by executing in various places.

How does TypeScript Extend Type work?

Let us see the working of a typescript with extending type in TypeScript which can be implemented by utilizing the ‘extends’ keyword; for that, we can interpret the section as given below,

Code:

type Element = {
  fname: string;
  durCreated: string;
  type: string;
};
interface UserElement extends Element {
  UserId: string;
}

Output:

Working TypeScript Extend Type

For generating the interface UserElement, we have to extend an Element type. When we try to append a string field ‘UserId’ in UserElement, it can take over the Element, in which we can say that the ‘extend’ keyword has been utilized to extend a type in typescript.

TypeScript Extend Interfaces Types

The interface is the keyword that can be utilized to state the typescript interface in which interface_name is the name of the interface, and the body of it can have the variables and methods; we can retrieve the interface from other interfaces in which we can say that typescript authorize an interface to take over by zero or more base types, and the base type can be a class or interface, the ‘extends’ keyword has been utilized to carry out the inheritance in the middle of the interfaces,

Let us see an example of it,

Syntax:

"child_interface extends parent interface
{
}"

Code:

interface Human
{   
   Fname: string  
   age:number  
}  
interface Worker extends Human {   
   gender: string  
   workCode:number  
}  
let wObject = {};   
wObject.FName = "Anay"  
wObject.age = 32   
wObject.gender = "Male"  
wObject.wCode = 43  
console.log("FName: "+wObject.FName);  
console.log("Worker Code: "+wObject.wCode);

Output:

TypeScript Extends Interfaces Type

There are mainly two types of typescript interface:

1. Array Type Interface

The interfaces can be utilized to define the array type; let us see an example of an array type interface that can return the string and also array has been used, then it will give back the number,

Code:

interface nameArray {  
    [index:number]:string  
}  
let ourNames: nameArray;  
ourNames = ['Viraj', 'Swar', 'Shivay'];  
  
interface ageArray {  
    [index:number]:number  
}  
var ourAges: ageArray;  
ourAges =[15, 21, 12];  
console.log("Our ages are: "+ourAges[1]);

Output:

Array Type Interface

In this example, first, we have stated the ‘nameArray,’ which can give back the string, and ‘ageArray,’ which can give back a number, the type of index in the array is every time a number; hence we can recover the array element with the utilization of their index position within the array, which can have the output which is given above.

2. Interface in Class

In typescript, we can utilize the interface in a class, and that can execute the interface with the ‘implements’ keyword, let us see an example to understand how it works,

Code:

interface Human {  
    fName: string;  
    lName: string;  
    age: number;  
    FullName();  
    GetAge();  
}  
class Agent implements Human {  
    fName: string;  
    lName: string;  
    age:number;  
    FullName() {  
        return this.fName + ' ' + this.lName;  
    }  
    GetAge() {  
        return this.age;  
    }  
    constructor(fN: string, lN: string, getAge: number) {
        this.fName = fN;  
        this.lName = lN;  
        this.age = getAge;  
    }  
}  
let myAgent = new Agent('Abhay', 'Chavan', 32);  
let fullName = myAgent.FullName();  
let Age = myAgent.GetAge();  
console.log("Name of Human: " +fullName + '\nAge: ' + Age);

Output:

Interface in Class

In this example, first, we have defined the Human interface with fname, lName, and FullName. The GetAge method has been used in which the Agent class has been executed this interface utilizing the ‘implements’ keyword, and then after utilization of the interface, we have to define the properties and methods within the class if we have not implemented that properties and methods then it will fetch an error, so we have also described the constructor within a class.

Examples

Different examples are mentioned below:

Example #1

In this example Profile interface extends to the Location interface:

Code:

interface Location {
    location: string;
    village: string
    dist: string
}
 
interface Organization {
    role: string;
}
 
interface Profile extends Location, Organization {
    Fname: string;
    Lname: string;
    Fname(): string;
}
 
 
let profile: Profile = {
    Fname: "Emil",
    Lname: "Andersson",
    Fname(): string {
        return this.Fname + " " + this.Lname;
    },
    location: "Karnataka",
    village: "Erode",
    dist: "Coimbatore",
    role: "CFO"
}

Output:

Profile Interface

Example #2

This typescript extend removes property, in which ‘age’ can also remove property from the interface, and is also able to remove various properties from the interface, and also remove property and extend the interface:

Code:

interface Human {
  name: string;
  age: number;
  location: string;
}
type WithoutAge = Omit<Human, 'age'>;
type WithoutAgeAndLocation = Omit<Human, 'age' | 'location'>;
interface HumanWithoutAge extends Omit<Human, 'age'> {
  language: string;
}

Output:

Removes Property

Conclusion

In this article, we conclude that the ‘extend’ keyword has been used to extend classes into interfaces; we have also discussed the working of typescript extend type, typescript extends interface types, and examples of the typescript extend type, so this article will help to understand the concept of typescript extend type.

Recommended Articles

This is a guide to TypeScript Extend Type. Here we discuss the introduction and how Extend Type works in TypeScript, along with interfaces and examples. You may also have a look at the following articles to learn more –

  1. TypeScript babel
  2. TypeScript enum
  3. TypeScript class
  4. TypeScript map

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW