lecture 10 of 120 completed

Modules and import type

TypeScript checks every import against what the other file exports. A type is imported like a value, and import type marks an import that exists only for the compiler.

What you'll learn

  • What the compiler checks on an import line
  • How a file shares a type, and how another file imports it
  • What import type means and why projects use it

You know modules from the JavaScript track. A file shares a name with export, and another file takes it with import. A wrong name in JavaScript fails when the file loads.

TypeScript reads both files first. Here is a file of fee helpers.

export interface Fee {
  days: number;
  amount: number;
}

export const DAILY_FEE = 0.5;

export function lateFee(days: number): Fee {
  return { days, amount: roundToCents(days * DAILY_FEE) };
}

function roundToCents(amount: number): number {
  return Math.round(amount * 100) / 100;
}

And a report that imports from it, with one letter in the wrong case.

import { latefee } from "./fees";

console.log(latefee(3).amount);
report.ts(1,10): error TS2724: '"./fees"' has no exported member named 'latefee'. Did you mean 'lateFee'?

The compiler looked inside fees.ts, found no export with that spelling, and suggested the closest one.

The path has no extension here

Look at the path: "./fees", with no .ts and no .js. Many TypeScript projects write imports this way, and a tool finds the file. Whether you may leave the extension out depends on a setting of the project. The JavaScript track told you that a browser and Node need the full file name, and that is still true for the JavaScript they run.

Follow what the project already does. If its imports have no extension, write yours the same way.

A name that is not exported

roundToCents has no export in front of it. It is private to fees.ts.

import { roundToCents } from "./fees";

console.log(roundToCents(1.005));
report.ts(1,10): error TS2459: Module '"./fees"' declares 'roundToCents' locally, but it is not exported.

The message is different from the first one. The name exists, and the file does not share it. Do not add export to make the error go away without thinking. Ask first whether your file needs that helper, or whether an exported function already does the job.

Types are exported too

Fee is an interface, and it has export in front of it. Another file imports it by name, like a function.

import { lateFee, Fee } from "./fees";

export function feeLine(fee: Fee): string {
  return fee.days + " days: " + fee.amount;
}

export const line: string = feeLine(lateFee(3));

That compiles. But the two imported names are different kinds of thing. lateFee is a function, and it exists when the code runs. Fee is a type. Lecture 1 said types are removed before the code runs, so at run time there is no Fee to import.

import type

import type states it: this import is only for the compiler.

import { lateFee } from "./fees";
import type { Fee } from "./fees";

export function feeLine(fee: Fee): string {
  return fee.days + " days: " + fee.amount;
}

export const sample: Fee = lateFee(3);

The whole second line is removed when the types are removed. A reader sees at once that nothing at run time depends on it.

The compiler enforces this. A name imported with import type cannot be used as a value.

import type { lateFee } from "./fees";

console.log(lateFee(3));
report.ts(3,13): error TS1361: 'lateFee' cannot be used as a value because it was imported using 'import type'.

Some projects turn on a setting that requires import type for every import that is only a type. Others leave the choice to you. In both, it is good practice: use import type for types and a plain import for everything that runs.

In a test file

Test files do this on their first lines. The test function is a value. The type of a page is a type.

import { test, expect } from "@playwright/test";
import type { Page } from "@playwright/test";

Lecture 11 uses that Page type.

Remember this

The compiler checks each imported name against the exports of the other file. A wrong spelling and a private name give different errors. Types are exported and imported like values, and import type marks an import that is removed before the code runs.

Reference: Modules in the TypeScript Handbook.

now prove it

Reading is the setup. Solve this exercise in the editor. We check your code with the TypeScript compiler and list each type error.