Advanced TypeScript for Harness Builders

0f. `import type` & the Type System at Compile Time

TypeScript types are erased at runtime. The compiled JavaScript has no types at all — that has consequences.

TypeScript types are erased at runtime. The compiled JavaScript has no types at all. This has important consequences.

Regular import (value + type):

File: packages/mom/src/tools/bash.ts L1-4

import { randomBytes } from "node:crypto"; // value -- exists at runtime
import { createWriteStream } from "node:fs"; // value -- exists at runtime
import { tmpdir } from "node:os"; // value -- exists at runtime
import { join } from "node:path"; // value -- exists at runtime

Type-only import (erased at runtime):

File: packages/mom/src/tools/bash.ts L5

import type { AgentTool } from "@mariozechner/pi-agent-core"; // type only -- gone at runtime

Why this matters:

  1. Bundle sizeimport type is completely removed, so it doesn’t pull in unused modules
  2. Circular dependencies — type-only imports can’t cause runtime circular dependency issues
  3. Isomorphic code — you can import Node.js types in browser code if they’re type-only

File: packages/ai/src/providers/openai-codex-responses.ts L1

import type * as NodeOs from "node:os"; // Type only -- no runtime import of node:os
let _os: typeof NodeOs | null = null; // Variable typed using that type-only import

This file runs in browsers too. The import type gives TypeScript the shape of node:os for type checking, but emits no require("node:os") call in the output.


Open this chapter inside the full course