How to export multiple Functions in TypeScript

avatar

Borislav Hadzhiev

Sun Mar 06 20223 min read

Export multiple Functions in TypeScript #

Use named exports to export multiple functions in TypeScript, e.g. export function A() {} and export function B() {}. The exported functions can be imported by using a named import as import {A, B} from './another-file'. You can have as many named exports as necessary in a single file.

Here is an example of exporting multiple functions from a file called another-file.ts.

another-file.ts
// 👇️ named export export function sum(a: number, b: number): number { return a + b; } // 👇️ named export export const multiply = (a: number, b: number): number => { return a * b; };

Note that using export on the same line as the function's definition is the same as exporting the functions as an object after they have been declared.

another-file.ts
function sum(a: number, b: number): number { return a + b; } const multiply = (a: number, b: number): number => { return a * b; }; // 👇️ named exports export { sum, multiply };

Here is how we would import the functions in a file called index.ts.

index.ts
// 👇️ named imports import { sum, multiply } from './another-file'; console.log(sum(10, 10)); // 👉️ 20 console.log(multiply(10, 10)); // 👉️ 100

Make sure to correct the path that points to the another-file module if you have to. The example above assumes that another-file.ts and index.ts are located in the same directory.

For example, if you were importing from one directory up, you would do import {sum, multiply} from '../another-file'.

We wrapped the names of the functions in curly braces when importing them - this is called a named import.

TypeScript uses the concept of modules, in the same way that JavaScript does.

In order to be able to import a function from a different file, it has to be exported using a named or default export.

The example above uses named exports and named imports.

The main difference between named and default exports and imports is - you can have multiple named exports per file, but you can only have a single default export.

If you try to use multiple default exports in a single file, you would get an error.

another-file.ts
// ⛔️ Error: A module cannot have // multiple default exports.ts(2528) export default function sum(a: number, b: number): number { return a + b; } const multiply = (a: number, b: number): number => { return a * b; }; export default multiply;

IMPORTANT: If you are exporting a variable (or an arrow function) as a default export, you have to declare it on 1 line and export it on the next. You can't declare and default export a variable on the same line.

Having said that, you can use 1 default export and as many named exports as you need in a single file.

Let's look at an example that exports multiple functions and uses both - default and named exports.

another-file.ts
// 👇️ default export export default function sum(a: number, b: number): number { return a + b; } // 👇️ named export export const multiply = (a: number, b: number): number => { return a * b; };

And here is how you would import the two functions.

index.ts
// 👇️ default and named import import sum, { multiply } from './another-file'; console.log(sum(10, 10)); // 👉️ 20 console.log(multiply(10, 10)); // 👉️ 100

Notice that we didn't wrap the default import in curly braces.

We used a default import to import the sum function and a named import to import the multiply function.

Note that you can only have a single default export per file, but you can have as many named exports as necessary.

In my experience, most real world codebases exclusively use named exports and imports, because they make it easier to leverage your IDE for autocompletion and auto-imports.

You also don't have to think about which members are exported with a default or named export.

Use the search field on my Home Page to filter through my more than 1,000 articles.