Site icon JD Bots

How to Import Only a Type with import type in TypeScript

A card titled import type, labelled TypeScript Basics, showing import { Book } failing with a SyntaxError and import { type Book } running.

import type: Import a type so it’s removed with the other types.

A TypeScript type only exists while the code is checked. When Node.js or a build tool removes the types to run the code, an import of a type would ask for something that isn’t there anymore. Writing type in front of it marks the import as type-only, so it’s removed with the types.

Every command and output on this page is real, run with Node.js 26.13.1 on macOS.

Syntax

Syntax

import type { TypeName } from "./file.ts";

import { type TypeName, value } from "./file.ts";

Node.js’s Modules: TypeScript page explains it under “Importing types without type keyword”.

Prerequisites

The Example

Two files in one folder: book.ts exports a Book type and a describe function, and main.ts imports both.

Step 1: Write book.ts

book.ts

export type Book = {
  title: string;
  pages: number;
};

export function describe(book: Book) {
  return book.title + " has " + book.pages + " pages.";
}

Book is a type, and describe is a real function.

Step 2: Import Both Without type

main.ts

import { Book, describe } from "./book.ts";

const book: Book = { title: "Salt and Starlight", pages: 320 };

console.log(describe(book));

Step 3: Run It

Terminal

node main.ts

Output (the lines that matter)

file:///Users/user/books/main.ts:1
import { Book, describe } from "./book.ts";
         ^^^^
SyntaxError: The requested module './book.ts' does not provide an export named 'Book'

Node.js removed the type from book.ts before running it, so the import asks for an export, Book, that isn’t there.

Step 4: Mark the Type

Write type in front of Book:

main.ts

import { type Book, describe } from "./book.ts";

const book: Book = { title: "Salt and Starlight", pages: 320 };

console.log(describe(book));

Run it again:

Terminal

node main.ts

Output

Salt and Starlight has 320 pages.

Node.js removed type Book along with the types and kept the import of describe.

Common Mistakes

MistakeWhat happensDo this instead
Putting type on a value: import { type describe }The import of describe is removed too, and calling it stops with ReferenceError: describe is not definedWrite type only in front of types

Troubleshooting

ProblemCauseFix
A warning that mentions MODULE_TYPELESS_PACKAGE_JSONThe folder has a package.json without a "type" fieldAdd "type": "module" to it, as the warning says

FAQ

Can tsc catch a type imported without type?

Yes, with its verbatimModuleSyntax option, which Node.js’s docs point to for the same rule.

Which form should I use?

import type { … } when everything you import is a type, and type in front of single names when one import brings in types and values together.

Conclusion

Mark imported types with type, as a whole import or name by name, so tools that remove types remove those imports too.

Additional Resources

TypeScript, Node.js and other names mentioned are trademarks of their respective owners.

Exit mobile version