TypeScript Declaration Files (.d.ts) and @types
What a .d.ts file is, where @types packages come from, how to type an untyped library, and "Could not find a declaration file for module".
In this chapter
Declaration files and @types
More on Namespaces, modules and declarations
Keep learning
Overview
chapters · namespaces and declarations Declaration files and @types A .d.ts file is a type-only description of JavaScript. Learn to read one, write one, and see how TypeScript finds them.
1. The problem: a package with no types
You install a small tip calculator. It is plain JavaScript. The import lights up red. import an untyped package Could not find a declaration file for module 'tip-calc'. '…/tip-calc/index.js' implicitly has an 'any' type. Try if it exists or add a new declaration (.d.ts) file containing Argument of type 'string' is not assignable to parameter of type 'number'. Six lines of types and the calls are checked again. '15%' is a string, but percent must be a number, so it does not fit.
2. A .d.ts is the shape without the body
JavaScript runs. The .d.ts next to it only describes it. TypeScript reads the .d.ts for types and writes no JavaScript from it. geo.js (runs) geo.d.ts (describes) [lat: number, lng: number] is a tuple: an array with a fixed length and a type per slot. The labels lat and lng are only names for readers.
a body in a .d.ts is an error
An implementation cannot be declared in ambient contexts. Trusted, not checked TypeScript never compares a .d.ts with the JavaScript. If it says number and the code returns a string, nobody notices. So keep them honest Generate them from real .ts source whenever you can: the tsconfig option declaration: true writes a .d.ts for each file.
3. declare: "this exists, trust me"
declare describes a value that something else creates: a script tag, a bundler, the runtime. you write it means e.g. injected by a <script> a global class exists types for an import path ambient module a global object with members add globals from a module see declaration merging
Card.tsx
logo: string Record<string, string> Type 'string' is not assignable to type 'number'. Record<string, string> means an object with string keys and string values. Line 4 fails because './logo.svg' matched '*.svg', so logo is a string, not a number. In Vite, the special comment own .d.ts, which already declares these wildcard modules.
5. How TypeScript finds types for a package
The search rules are called module resolution. TypeScript checks these places in order. DefinitelyTyped is a community project that publishes types as @types/… packages. "exports" → "types" modern packages "types" / "typings" field older packages index.d.ts next to main by file name TS7016: implicitly any Stops at the first hit. If all fail you get TS7016, the error from section 1. Scoped packages map to @types/scope__name (e.g. @types/babel__core). exports + types Modern. Read by moduleResolution bundler, node16 and nodenext. Put "types" first by convention. old resolver Skips checking all .d.ts files, yours too: the body above goes unreported. Keep hand-written types in .ts files when you can.
tsconfig.json (library)
type not exported Users cannot import BookingOptions. To name it they must write Parameters<typeof book>[0]: "the type of book's first parameter". export every public type If it appears in a public signature, export it.
Remember
declare says a value exists somewhere else. declare module '*.svg' types non-code imports. Types are found via exports "types", the types field, then @types. skipLibCheck skips all .d.ts, including yours. ! Cheat code → Go deeper: declaration merging, module resolution, tsconfig flags, migrating JavaScript to TypeScript.
More courses
- Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
- Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
- React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
- Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
- System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
- NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
- Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
- Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
- AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
- Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
- CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
- Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
- Docker — What an image is, why layers cache, and how containers find each other on a network.
- Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
- Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
- AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
- Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.