Import a Type from Another file using TypeScript

avatar

Borislav Hadzhiev

Fri Mar 04 20223 min read

Import a Type from Another file using TypeScript #

To import a type from another file in TypeScript:

  1. Export the type from file A, e.g. export type Employee = {}.
  2. Import the type in file B as import { Employee } from './another-file';.
  3. Use the type in file B.

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

another-file.ts
// 👇️ named export export type Employee = { id: number; name: string; salary: number; };

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

index.ts
// 👇️ named import import { Employee } from './another-file'; const emp: Employee = { id: 1, name: 'James', salary: 100, }; console.log(emp);

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 {Employee} from '../another-file'.

We wrapped the name of the type in curly braces when importing it - 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 type from a different file, it has to be exported using a named or default export.

You might also see examples that explicitly use the type keyword when importing.

index.ts
// 👇️ explicitly importing type import type { Employee } from './another-file'; const emp: Employee = { id: 1, name: 'James', salary: 100, }; console.log(emp);

You can only use the import type syntax when importing types and interfaces.

The import type syntax is mostly used to get around some circular import errors caused by importing types between the same files.

The examples above use 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.

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

Here are the contents of another-file.ts.

another-file.ts
type Employee = { id: number; name: string; salary: number; }; // 👇️ default export export default Employee;

And here is how we would import the type using a default import.

index.ts
// 👇️ default import import Employee from './another-file'; const emp: Employee = { id: 1, name: 'James', salary: 100, }; console.log(emp);

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

We could have also used a different name when importing the type, e.g. Foo.

index.ts
// 👇️ default import import Foo from './another-file'; const emp: Foo = { id: 1, name: 'James', salary: 100, }; console.log(emp);

This works, but is confusing and should be avoided.

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.

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
type Employee = { id: number; name: string; salary: number; }; // 👇️ default export export default Employee; // 👇️ named export export type Person = { name: string; };

And here is how you would import the two types.

index.ts
// 👇️ default and named imports import Employee, { Person } from './another-file'; const emp: Employee = { id: 1, name: 'James', salary: 100, }; console.log(emp); const person: Person = { name: 'Carl', }; console.log(person);

We used a default import to import the Employee type and a named import to import the Person type.

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

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