EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript Export Function
Secondary Sidebar
TypeScript Tutorial
  • Function Of Array
    • TypeScript Function Interface
    • TypeScript Functions
    • TypeScript Export Function
    • TypeScript function return type
  • TypeScript Basic and Advanced
    • What is TypeScript?
    • Typescript Examples
    • TypeScript Versions
    • TypeScript Operators
    • JavaScript dump object
    • JavaScript get Method
    • Webpack ReactJS
    • Code Generator JavaScript
    • JavaScript Projects
    • Call Stack JavaScript
    • JavaScript Projects GitHub
    • JavaScript Filter Function
    • JavaScript nan
    • JavaScripttimestamp
    • TypeScript loop
    • CoffeeScript
    • TypeScript Webpack
    • setTimeout TypeScript
    • DHTMLX
    • CoffeeScript for loop
    • TypeScript number
    • JavaScript export module
    • TypeScript string contains
    • TypeScript Inheritance
    • TypeScript get
    • TypeScript undefined
    • TypeScript Global Variable
    • TypeScript Dictionary
    • TypeScript Generic
    • TypeScript Cast Object
    • TypeScript Optional Parameters
    • TypeScript? switch
    • TypeScript promise
    • TypeScript tuple
    • TypeScript Hashmap
    • TypeScript let
    • TypeScript Getter
    • TypeScript Pattern Matching
    • TypeScript number to string
    • TypeScript substring
    • TypeScript?lambda
    • TypeScript UUID
    • TypeScript JSDoc
    • TypeScript Decorators
    • Typescript for loop
    • TypeScript HTTP Request
    • TypeScript Abstract Class
    • TypeScript Question Mark
    • TypeScript Nullable
    • TypeScript reduce
    • TypeScript Mixins
    • TypeScript keyof
    • TypeScript string to number
    • TypeScript JSON parse
    • TypeScript const
    • TypeScript declare module
    • TypeScript String
    • TypeScript filter
    • TypeScript Multiple Constructors
    • TypeScript? Set
    • TypeScript string interpolation
    • TypeScript instanceof
    • TypeScript JSON
    • TypeScript Arrow Function
    • TypeScript generator
    • TypeScript namespace
    • TypeScript default parameter
    • TypeScript cast
    • TypeScript babel
    • Typescript Key-Value Pair
    • TypeScript if
    • TypeScript keyof Enum
    • TypeScript wait
    • TypeScript Optional Chaining
    • TypeScript JSX
    • TypeScript Version Check
    • TypeScript Unit Testing
    • TypeScript Handbook
    • TypeScript module
    • TypeScript Extend Interface
    • TypeScript npm
    • TypeScript pick
    • TypeScript Interface Default Value
    • JavaScript import module
    • Obfuscate Javascript
    • TypeScript basics
    • setInterval TypeScript
  • Type of Union
    • TypeScript Object Type
    • TypeScript type check
    • TypeScript promise type
    • TypeScript JSON type
    • TypeScript Union Types
    • TypeScript typeof
    • TypeScript Types
  • TypeScript Array
    • TypeScript Array of Objects
    • Methods TypeScript Array
    • TypeScript remove item from array
    • TypeScript add to array
    • TypeScript Array Contains

TypeScript Export Function

By Shalu SharmaShalu Sharma

TypeScript Export Function

Definition of TypeScript Export Function

In TypeScript we can almost export anything using the ‘export’ keyword. It is not a function rather it can be used to export several things in TypeScript. By the use of an export keyword, we can export class, function, file, interface, type, etc. in TypeScript. By default it comes up with all the classes or interfaces we create in TypeScript, after this we can easily import these files using the ‘import’ keyword. Export helps us to use the existing component in the while application at any number of times. In the coming section, we will discuss more the export function in detail for better understanding.

Syntax

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

As this is the keyword that can be used with function, class, interface, etc. in TypeScript. Lets’ see its syntax for a better understating of the export keyword in TypeScript see below;

  • export class Your_class_name: This syntax is used to export the class in TypeScript, we are using the ‘export’ keyword after this we can easily export this class anywhere in the application to reuse the existing functionality.
  • export function function_name: This syntax is used to export the function in TypeScript. Yes, we can export the functions in TypeScript by using the ‘export’ keyword at the start of the function.
  • export interface inteface_name: In TypeScript, we can export the interface as well, we can follow this syntax to make the interface exportable.

As you can see in the above lines of syntax we are using the ‘export’ keyword to make them exportable in the application, in the coming section, we will some more practical example of how to implement them in the program to get a better understanding of its usage in detail.

How does Export Function Work in TypeScript?

As now we already know that export keyword is used to export the classes, interface, functions, and type in TypeScript and made them available to reuse the component by importing them inside any other module by using the import statement at the beginning of the TypeScript program. As we can see in the angular library we are able to import them the reason behind is that they are exportable in nature. All the classes in the annular library are already made exportable by using the ‘export’ keyword. In this section first, we will see how to export the component, function in TypeScript with the signature of the syntax in detail followed by the practice example for each of them. Let’s discuss each of them in detail see below;

1. Export Function

In TypeScript, we can export a function from the whole class. For this, we have to use the export keyword at the initial of the function declaration. After this, we can use the import statement to import them inside any other module. Below we will see its syntax and one practice example for beginners to understand it better how we can use this while programming in TypeScript see below;

export function function_name(){}

Example:

export function mydemo(){
// logic will go here ..
console.log("this is exportable function in TypeScript. !!")
}

In the above lines of code, we are creating one sample function to understand the usage of export with function in TypeScript. what we have done is we are creating one function and making it exportable by using the ‘export’ keyword. We are using this keyword at the start of the function declaration. Now in any class, we can import this function and use it as it is. to import this function we can follow the below steps/ path mentioned see below;

Example:

import {function_name} from ./path_to _file;

2. Export Class

In TypeScript we can export a class we can say a complete component. For this, we have to use the export keyword at the initial of the class declaration. After this, we can use the import statement to import them inside any other module. Below we will see its syntax and one practice example for beginners to understand it better how we can use this while programming in TypeScript see below;

export class class_name(){}

Example:

export class Demo{
// logic will go here ..
}

In the above lines of code, we are creating one sample class to understand the usage of export with class in TypeScript. what we have done is we are creating one class and making it exportable by using the ‘export’ keyword. We are using this keyword at the start of the class declaration. Now in any class, we can import this class and use as it is. To import this class we can follow the below steps/ path mentioned see below;

Example:

import {class_name} from ./path_to _file;

3. Export Interface

In TypeScript we can export an interface we can say a complete component. For this, we have to use the export keyword at the initial of the interface declaration. After this, we can use an import statement to import them inside any other module. Below we will see its syntax and one practice example for beginners to understand it better how we can use this while programming in TypeScript see below;

export class interface_name(){}

Example:

export interface DemoInterface{
// logic will go here ..
}

In the above lines of code, we are creating one sample class to understand the usage of export with class in TypeScript. what we have done is we are creating one class and making it exportable by using the ‘export’ keyword. We are using this keyword at the start of the class declaration. Now in any class, we can import this class and use it as it is. To import this interface we can follow the below steps/ path mentioned see below;

Example:

import {interface_name} from ./path_to _file;

Example of TypeScript Export Function

In this example we are trying to use export from Typescript, it is not a function rather it is a keyword that can be sued with a function as well to make them reusable in our Typescript application. Below is one sample example for beginners to understand export in detail see below;

Example:

export class DemoExport {
DemoExport(){
console.log("hello");
}
getmessage(msg : string){
console.log("message is  ::" + msg);
}
}
let z = new DemoExport();
console.log(z);
console.log("Demo to show working of export in Typescript !!!");
z.getmessage("message one from export  demo in typescript !!");
z.getmessage("message two from export  demo in typescript !!");
z.getmessage("message three from export  demo in typescript !!");
z.getmessage("message four from export  demo in typescript !!");
z.getmessage("message five from export  demo in typescript !!");

Output:

TypeScript Export Function-1.1

Conclusion

In this way, we can use the export keyword in TypeScript. This is very useful when we want to re-use our component made in TypeScript. After exporting we can easily import them inside any class or in the module itself to make it global for the whole application. This will increase the reusability of the function, class, interface, and type in TypeScript.

Recommended Articles

We hope that this EDUCBA information on “TypeScript Export Function” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. TypeScript Functions
  2. TypeScript Operators
  3. TypeScript Versions
  4. TypeScript Array
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - 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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*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?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more