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
import type { TypeName } from "./file.ts";
import { type TypeName, value } from "./file.ts";
import type { … }: everything in the braces is a type, and the whole import is removed.import { type TypeName, value }:typein front of one name marks just that name as a type. The other names stay real imports."./file.ts": the file that exports them.
Node.js’s Modules: TypeScript (opens in a new tab) page explains it under “Importing types without type keyword”.
Prerequisites
- Node.js 24.12 or later, which runs TypeScript files directly. Install it from the Node.js download page (opens in a new tab).
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
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
import { Book, describe } from "./book.ts";
const book: Book = { title: "Salt and Starlight", pages: 320 };
console.log(describe(book));
Step 3: Run It
node main.ts
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:
import { type Book, describe } from "./book.ts";
const book: Book = { title: "Salt and Starlight", pages: 320 };
console.log(describe(book));
Run it again:
node main.ts
Salt and Starlight has 320 pages.
Node.js removed type Book along with the types and kept the import of describe.
Common Mistakes
| Mistake | What happens | Do 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 defined | Write type only in front of types |
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
A warning that mentions MODULE_TYPELESS_PACKAGE_JSON | The folder has a package.json without a "type" field | Add "type": "module" to it, as the warning says |
FAQ
Can tsc catch a type imported without type?
Yes, with its verbatimModuleSyntax (opens in a new tab) 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
- Modules: TypeScript (opens in a new tab) (Node.js docs)
- TypeScript 3.8 (opens in a new tab): type-only imports and exports (TypeScript docs)
- TypeScript 4.5 (opens in a new tab): type modifiers on import names (TypeScript docs)
TypeScript, Node.js and other names mentioned are trademarks of their respective owners.
0 comments