TypeScript · Lesson 74 of 120
Type-Only Imports
Import type.
Some imports are only types. They disappear completely after compiling. Writing import type makes that clear, and guarantees no runtime code is loaded.

- 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.
| key | value |
|---|---|
| import type | types only |
| { type X } | mixed imports |
| Setting | verbatimModuleSyntax |
| Benefit | no 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