Modules & Declaration Files — TypeScript

export makes what's in a file usable from other files, and import { name } from "./file" takes it. import type brings only types, and disappears completely…

You install a JavaScript library from npm, then import one of its functions into a .ts file. A red line appears on the import, even though the library works fine in JavaScript. TypeScript can't guess types from that library's JavaScript code, so it needs them explained.

You explain them in a .d.ts file, which holds nothing but types. This file is called a declaration file. It's like the manual in a rice cooker's box: it tells you which buttons there are and what they do, but the manual isn't the machine. The manual is the .d.ts file, and the machine is the library's JavaScript code. TypeScript reads the manual, JavaScript does the work. This lesson also covers one thing TypeScript adds to modules: an import that only brings types. The rest is just like JavaScript modules: every file shares what's in it through export and import.

export and import

export makes something in a file usable from other files. Without export, it can only be used inside that file. In TypeScript, types and interfaces can be exported too. An example with three files:

// src/types.ts
export interface User {
  name: string
  age: number
}

// src/pair.ts
export function createPair<A, B>(a: A, b: B): [A, B] {
  return [a, b]
}

// src/app.ts
import { createPair } from "./pair"
import type { User } from "./types"

const budi: User = { name: "Budi", age: 17 }
const pair = createPair(budi.name, budi.age) // pair: [string, number]
console.log(pair) // ["Budi", 17]

The path (the file's address) in import can be written in three ways. The right one depends on what runs the code:

Path When to pick it
"./pair" A Vite project or another bundler (a tool that joins code files together). The examples in this lesson use this form.
"./pair.js" Node code compiled with tsc and the tsconfig (TypeScript settings file) option "module": "nodenext". Write .js even though the file is pair.ts, because pair.js is what runs later. Without an extension, tsc refuses with error TS2835: Did you mean './pair.js'?
"./pair.ts" Node running .ts directly (node app.ts). Enable the option rewriteRelativeImportExtensions, so that tsc writes it as ./pair.js in the compiled output.

import type: gone after compiling

User is only a type, so there's nothing to import when the code runs. import type says this clearly. Here's the compiled app.js:

import { createPair } from "./pair"

const budi = { name: "Budi", age: 17 }
const pair = createPair(budi.name, budi.age)
console.log(pair)

The import type line is gone completely, and types.ts becomes a types.js that holds only export {}. The Vite project templates enable the tsconfig option verbatimModuleSyntax. With this option, TypeScript leaves every import not marked type exactly as written in the compiled output. So import { User } from "./types" is rejected:

error TS1484: 'User' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.

If that line stayed, it would end up in the JavaScript. But types.js has nothing called User, so app.js would fail to load.

A library without types: .d.ts and declare module

format-price in this example is a plain JavaScript library. All it has is index.js, which exports the function format in Node's older way (exports.format = ...):

// node_modules/format-price/index.js
exports.format = function (amount) {
  return "$" + amount.toLocaleString("en-US")
}

TypeScript can't guess types from JavaScript code in node_modules (the folder where npm installs libraries). So the import is rejected:

error TS7016: Could not find a declaration file for module 'format-price'. '/app/node_modules/format-price/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/format-price` if it exists or add a new declaration (.d.ts) file containing `declare module 'format-price';`

The fix is a .d.ts file you write yourself, for example src/format-price.d.ts:

declare module "format-price" {
  export function format(amount: number): string
}

Now TypeScript knows format takes a number:

Types for format-price format(1500) format("1500")
none yet error TS7016 error TS7016
the full .d.ts above OK, prints $1,500 error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
the short form declare module "format-price"; OK, prints $1,500 no error, prints $1500

The short form removes TS7016, but everything in the module becomes any. The text "1500" is accepted, and the result is wrong with no error: the thousands comma is missing.

@types: .d.ts files written by the community

For popular libraries, someone has usually written the .d.ts already. The DefinitelyTyped project publishes them as @types/library-name packages:

npm install --save-dev @types/lodash

--save-dev marks the package as needed only while you write and compile the code, not when the program runs. When you import lodash, TypeScript looks for its types in node_modules/@types automatically, so you don't import the @types package. Many modern libraries (axios, zod) even include their own .d.ts, so their types are ready right away. The order to try:

  1. Check whether the library has its own types.
  2. If not, look for @types/....
  3. If there's none, write your own declare module.

This automatic search in node_modules/@types has an exception: global types, which are types you use without import, such as process (an object that belongs to Node.js) from @types/node. Since TypeScript 6.0, global types are not loaded automatically, so write "types": ["node"] in compilerOptions (the compiler settings part of tsconfig). Without it, process.version is rejected: Cannot find name 'process'. ts(2591).

⚠️ Imports that get refused, and a .d.ts with too much in it

TL;DR: export makes what's in a file usable from other files, and import { name } from "./file" takes it. import type brings only types, and disappears completely from the compiled JavaScript. In a project with the option verbatimModuleSyntax (like a Vite project), importing a type without the word type is error TS1484. A .d.ts file (a declaration file) holds types with no function bodies, and produces no JavaScript. Importing a JavaScript library that has no types causes error TS7016. Install @types/library-name from DefinitelyTyped (a community project that writes types for libraries) if it exists, or write declare module "library-name" { ... } yourself. The short form declare module "library-name"; turns everything in that library into any.

⚡ import type is gone after compiling

Write:

import { createPair } from "./pair"
import type { User } from "./types"

Meaning: createPair is a function exported from the file pair.ts, and User is an interface from types.ts. export lets other files take what's in a file, types and interfaces included. User is only a type, so the import type line disappears completely from the compiled JavaScript. In a Vite project the option verbatimModuleSyntax is enabled, so importing a type without the word type is rejected with error TS1484.

⚡ A library without types: write a .d.ts

Write:

declare module "format-price" {
  export function format(amount: number): string
}
Guess first: format-price is a JavaScript library with no types. If you write only the short form declare module "format-price";, is format("1500") rejected?

No. The short form makes everything in it any, so text is accepted, and the result is wrong with no error: $1500, without the thousands comma.

Meaning: A .d.ts file (a declaration file) describes the library's types to TypeScript. declare means the code already exists somewhere else, and you're only describing its types. There's no function body, and a .d.ts file produces no JavaScript at all.

⚡ Look for @types first

Write:

npm install --save-dev @types/lodash

Meaning: For popular libraries, the DefinitelyTyped community has usually written the .d.ts already, and publishes it as an @types/... package. The order: check whether the library includes its own types, then look for @types/..., and only then write declare module yourself. --save-dev marks the package as needed only while you write and compile the code.

What you will learn