AI Hints
TypeScript · Lesson 74 of 120

Type-Only Imports

Import type.

⏱ 2 min read▶ Video at 56:53🎓 Free
Watch this lesson in the TypeScript courseWatch on YouTube ↗Full TypeScript course

Some imports are only types. They disappear completely after compiling. Writing import type makes that clear, and guarantees no runtime code is loaded.

Type-Only Imports
From the video: Type-Only Imports
  • Only types
  • Disappear
  • import type

import type

We import only the money type. Because it's import type, the compiled JavaScript has no import at all. Run it.

app.tstypescript
import type { Money } from "./pricing.js";

const refund: Money = { amount: 65, currency: "GBP" };
console.log("Refund:", refund.amount, refund.currency);
Output
Refund: 65 GBP
Also uses pricing.ts
export const VAT = 0.2;

export function withVat(price: number): number {
  return Math.round(price * (1 + VAT) * 100) / 100;
}

export type Money = { amount: number; currency: "GBP" | "EUR" };

When to use it

Use import type, or the type keyword inside the braces, for anything that's only a type. Many projects turn on a setting that requires it, called verbatim module syntax. It keeps builds fast and predictable.

keyvalue
import typetypes only
{ type X }mixed imports
SettingverbatimModuleSyntax
Benefitno surprise imports

Summary

Use import type for types, so no runtime code is loaded. Next, declaration files.

Key takeaways
  • import type
  • { type Money }
  • Erased
Next up: Declaration Files — .d.ts