How to Import Values from Another file in TypeScript

avatar
Borislav Hadzhiev

Last updated: Feb 27, 2024
4 min

banner

# Table of Contents

  1. Import Values from Another file in TypeScript
  2. Using a default export and import in TypeScript
  3. Using both named and default exports
  4. Importing Classes from another file in TypeScript
  5. Importing Enums from another file in TypeScript

# Import Values from Another file in TypeScript

To import values from another file in TypeScript:

  1. Export the values from file A using a named export.
  2. Import the values in file B using a named import.
  3. Use the imported values in file B.

Here is an example of exporting a function and a variable from a file called another-file.ts.

another-file.ts
// ๐Ÿ‘‡๏ธ named export export function sum(a: number, b: number) { return a + b; } // ๐Ÿ‘‡๏ธ named export export const site = 'bobbyhadz.com'
The code for this article is available on GitHub

The syntax is the same when exporting, classes, types, interfaces and enums.

All you have to do is prefix the value with the export keyword.

Here is how we would import the sum function and the variable in a file called index.ts.

index.ts
// ๐Ÿ‘‡๏ธ named import import { sum, site } from './another-file'; console.log(sum(50, 10)); // ๐Ÿ‘‰๏ธ 60 console.log(site); // ๐Ÿ‘‰๏ธ bobbyhadz.com

import values from another file in typescript

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 another-file.ts was located one directory up, you'd have to import as import {sum, site} from '../another-file'.

We wrapped the name of the function and variable 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 a default export.

The example above uses named exports and named imports.

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

# Using a default export and import in TypeScript

Let's look at an example of how we would import a value that was exported using a default export.

Here are the contents of another-file.ts.

another-file.ts
// ๐Ÿ‘‡๏ธ default export export default function sum(a: number, b: number) { return a + b; }

Here is how we would import the function using a default import.

index.ts
// ๐Ÿ‘‡๏ธ default import import sum from './another-file'; console.log(sum(50, 10)); // ๐Ÿ‘‰๏ธ 60

using default export and import in typescript

The code for this article is available on GitHub

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

We could have also used a different name when importing the function, e.g. foo.

index.ts
import foo from './another-file'; console.log(foo(50, 10)); // ๐Ÿ‘‰๏ธ 60

This works but is confusing and should be avoided.

NOTE: 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.

index.ts
const multiply = (a: number, b: number) => { return a * b; }; export default multiply;
In my experience, most real-world codebases exclusively use named exports and imports because they make it easier to leverage your IDE for auto-completion and auto-imports.

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

# Using both named and default exports

You can also mix and match, here is an example of a file that uses both a default and a named export.

another-file.ts
// ๐Ÿ‘‡๏ธ default export export default function sum(a: number, b: number) { return a + b; } // ๐Ÿ‘‡๏ธ named export export const site = 'bobbyhadz.com'

And here is how you would import the values from a different file.

index.ts
// ๐Ÿ‘‡๏ธ default and named imports import sum, { site } from './another-file'; console.log(sum(50, 10)); // ๐Ÿ‘‰๏ธ 60 console.log(site); // ๐Ÿ‘‰๏ธ bobbyhadz.com

using both named and default exports

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.

# Importing Classes from another file in TypeScript

The same syntax can be used to import a class from a different file.

another-file.ts
// ๐Ÿ‘‡๏ธ named export export class Employee { constructor(public id: number, public name: string, public salary: number) { this.id = id; this.name = name; this.salary = salary; } getSalary() { return this.salary; } }

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

index.ts
// ๐Ÿ‘‡๏ธ named import import { Employee } from './another-file'; const emp1 = new Employee(1, 'Bobby Hadz', 100); console.log(emp1.name); // ๐Ÿ‘‰๏ธ "Bobby hadz" console.log(emp1.getSalary()); // ๐Ÿ‘‰๏ธ 100

# Importing Enums from another file in TypeScript

The same syntax can also be used to import an enum from another file.

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

another-file.ts
// ๐Ÿ‘‡๏ธ named export export enum EmailStatus { Read = 'READ', Unread = 'UNREAD', Draft = 'DRAFT', }

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

index.ts
// ๐Ÿ‘‡๏ธ named import import { EmailStatus } from './another-file'; const draft = EmailStatus.Draft; console.log(draft); // ๐Ÿ‘‰๏ธ "DRAFT"

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev