Sub topic 3
import type and export type
This chapter assumes you can write ES module import and export statements, run tsc, and edit tsconfig.json. It covers how to mark an import or export as type-only, what the compiler does with that marker, and which errors you will see when you get it wrong.
Why type-only syntax exists
A TypeScript file contains two kinds of names: values (functions, constants, classes) and types (interfaces, type aliases). Types exist only during checking. When tsc emits JavaScript, it removes every type.
An import such as import { User } from "./user.js" does not say which kind User is. The compiler has to work it out. By default it keeps an import if the name is used as a value, and drops it if the name is only used in type positions. This is called import elision.
Elision works when the compiler can see the whole program. It fails in tools that compile one file at a time, such as Babel, esbuild, and ts.transpileModule. These tools cannot look inside ./user.js to learn whether User is a type. They need the source code to say so. Type-only syntax is how you say so.
import type
An import type statement imports names that can be used only in type positions.
// user.ts
export interface User {
id: number;
name: string;
}
export function createUser(name: string): User {
return { id: Date.now(), name };
}
// profile.ts
import { createUser } from "./user.js";
import type { User } from "./user.js";
const u: User = createUser("Ada");
console.log(u.name);The compiler erases the import type line completely. The emitted profile.js has only the import of createUser.
You can also import a default or a namespace as a type:
import type Logger from "./logger.js";
import type * as Models from "./models.js";
let log: Logger;
let m: Models.User;Classes imported as types
A class is both a value and a type. If you write import type { Account } for a class, you get only the type side. You can annotate with it, but you cannot call new Account() or use instanceof.
import type { Account } from "./account.js";
function show(a: Account): string {
return a.owner;
}
// const a = new Account("Ada");
// error TS1361: 'Account' cannot be used as a value
// because it was imported using 'import type'.This restriction is the point. The compiler can now prove the import is unused at runtime without reading account.ts.
Inline type modifiers
You can mix values and types in one statement by putting type in front of individual names.
import { createUser, type User } from "./user.js";Both forms describe the same program. Choose by how much you want to keep at runtime:
| Form | Runtime effect under `verbatimModuleSyntax` |
|---|---|
| `import type { User } from "./user.js"` | The whole statement is removed. The module is not loaded. |
| `import { type User } from "./user.js"` | The statement becomes `import {} from "./user.js"`. The module still loads. |
| `import { createUser, type User } from "./user.js"` | Becomes `import { createUser } from "./user.js"`. |
The second row matters. The inline form with only type names keeps the module's side effects, because the import statement stays. The import type form guarantees no load happens. Prefer import type when every name is a type.
export type
The same idea applies to exports. Use export type when you re-export something that exists only as a type.
// index.ts
export { createUser } from "./user.js";
export type { User } from "./user.js";You can also export a local name as a type-only export:
interface Config {
debug: boolean;
}
export type { Config };Since TypeScript 5.0, you can re-export everything from a module as types:
export type * from "./user.js";
export type * as UserTypes from "./user.js";Everything re-exported this way is usable in type positions only, even functions and classes in user.ts. Importers cannot call them through this path.
What the compiler erases
Because the statement vanishes, import type also helps with circular dependencies. If a.ts and b.ts import each other only for types, switching to import type removes the runtime cycle. The type references still resolve during checking.
verbatimModuleSyntax and isolatedModules
Two compiler options decide how strict the compiler is about this syntax.
{
"compilerOptions": {
"target": "es2022",
"module": "nodenext",
"strict": true,
"verbatimModuleSyntax": true,
"outDir": "dist"
},
"include": ["src"]
}With verbatimModuleSyntax on, the rule is simple: any import or export without type is kept in the output, and anything marked type is dropped. The compiler stops guessing. It reports an error when you import a type without marking it:
import { User } from "./user.js";
// error TS1484: 'User' is a type and must be imported using a
// type-only import when 'verbatimModuleSyntax' is enabled.isolatedModules is the older, narrower option. It warns about code that a single-file compiler cannot handle correctly. The main case is re-exporting a type without type:
export { User } from "./user.js";
// error TS1205: Re-exporting a type when 'isolatedModules' is enabled
// requires using 'export type'.verbatimModuleSyntax supersedes the older importsNotUsedAsValues and preserveValueImports options, which were deprecated in TypeScript 5.0. Use it for new projects. It also implies the checks that isolatedModules makes for type re-exports.
Common errors and fixes
- TS1361, "cannot be used as a value because it was imported using 'import type'". You used a type-only name in a value position, such as
new, a function call, orinstanceof. Change the statement to a normalimportif you need the value. - TS1484, "must be imported using a type-only import". The name is only a type. Add
typeto the import. - TS1205, "Re-exporting a type when 'isolatedModules' is enabled". Change
export { T } fromtoexport type { T } from. - A runtime "module not found" error for a file you only used for types. You wrote a normal import where the compiler used to elide it, then turned on
verbatimModuleSyntax. Mark the importtype.
A rule of thumb
Mark an import type when you use the name only in annotations, generics, extends clauses of interfaces, and typeof-free type positions. Leave it unmarked when you call it, construct it, or read it at runtime. Doing this by habit keeps your output predictable and keeps your code working with any tool that compiles files one at a time.
Exercise
A file a.ts contains import { Config } from "./config.js"; and uses Config only in function load(c: Config) {}. config.ts exports Config as an interface. With verbatimModuleSyntax enabled, what error do you get, how do you fix it with the shortest change, and what does the emitted a.js import afterwards?
Show answer
You get TS1484: Config is a type and must be imported using a type-only import. Change the line to import type { Config } from "./config.js";. The emitted a.js has no import of ./config.js, because the whole statement is erased. Writing import { type Config } would also compile, but it would leave import {} from "./config.js" in the output and load the module.
Exercise
You have export { User, createUser } from "./user.js"; in an index.ts where User is an interface and createUser is a function. Rewrite it so isolatedModules accepts it, and say what importers of index.ts can do with each name.
Show answer
Write export { createUser } from "./user.js"; and export type { User } from "./user.js";. Importers can call createUser and use User in type positions. Another option is export { createUser, type User } from "./user.js";, which behaves the same.