Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
1 min readTypeScript

Migrate to verbatimModuleSyntax

Migrate to verbatimModuleSyntax by marking type-only imports and matching your import syntax to your output format.

When you turn on verbatimModuleSyntax, TypeScript reports TS1484 on unmarked imports of type-only symbols. The fix depends on whether the import also carries a runtime value.

TypeScript 5.0 added verbatimModuleSyntax and deprecated importsNotUsedAsValues and preserveValueImports in its favour.

What the flag does

With the flag enabled, TypeScript keeps value imports and exports and erases bindings marked type. A declaration-level import type disappears entirely. An inline-only import { type User } from './types.js' leaves import {} from './types.js', which still loads the module.

For this example, put export interface User with id and name string fields in src/types.ts. Add export const greet = (user: User) => user.name there. Save this example as src/main.ts and run tsc --module esnext --verbatimModuleSyntax src/main.ts from a scratch project without a tsconfig.json.

import { User, greet } from './types.js'

const u: User = { id: '1', name: 'Ana' }
console.log(greet(u))
src/main.ts(1,10): error TS1484: 'User' is a type and must be imported
using a type-only import when 'verbatimModuleSyntax' is enabled.

User only exists in TypeScript. Mark the import so the name disappears from the emitted JavaScript.

The fix is one word

Mark the type in the import.

import { type User, greet } from './types.js'

The emitted file reads import { greet } from './types.js'; with User gone and greet kept. When a whole import is types, write import type { User } from './types.js' and the line disappears from the output.

Replacing the old flag

In tsconfig.json, delete importsNotUsedAsValues and preserveValueImports, add "verbatimModuleSyntax": true, run tsc, and add type wherever TS1484 points. The error lists every spot, so the migration is mechanical.

The flag also refuses to rewrite import and export to require and module.exports when the file compiles to CommonJS. If your output is CommonJS, write import foo = require("foo") and export = in those files, or move the project to ES modules.

June 2024 update. TypeScript 5.5 disabled the old flags. Remove both from your config when upgrading.