What are .d.ts declaration files and triple-slash directives in TypeScript?
IntermediateTypeScript interview questionBy UIRound, updated 21 November 2026
Short answer
A .d.ts declaration file describes the types of JavaScript code without containing any implementation: function signatures, classes, interfaces and module shapes. TypeScript reads it to type-check code that uses a library, and emits nothing for it. You get them from the compiler (declaration: true), from @types packages or from the library itself, or you write them by hand. Triple-slash directives such as /// <reference types="vite/client" /> are an older way to pull declarations into a compilation.
What a declaration file contains
Only types and declare statements. A declare tells the compiler that a value exists at runtime without providing it.
// format.js
export function formatPrice(cents, currency) { /* ... */ }
// format.d.ts
export declare function formatPrice(cents: number, currency: string): string;
export interface PriceOptions {
currency: string;
showCents?: boolean;
}When code imports ./format, TypeScript finds format.d.ts next to format.js and uses it. A .d.ts file cannot contain function bodies, variable initialisers or any other code that would run.
Where declarations come from
- Generated: with
"declaration": trueintsconfig.json,tscwrites a.d.tsfile next to each emitted.jsfile."emitDeclarationOnly": trueis common when a bundler produces the JavaScript, and"declarationMap": truelets editors jump from a declaration to the original source. - Bundled with the package: a library points to its types with the
typesfield inpackage.json, or atypescondition inexports, which must come first in each conditions object. - DefinitelyTyped: for libraries written in plain JavaScript, the community publishes types as
@types/<name>, for example@types/lodash.
By default, every package in node_modules/@types is included automatically, which matters for packages that add globals, such as @types/node or test runner globals. "types": ["node", "vitest/globals"] limits the automatic inclusion to that list, and typeRoots changes which folders count. Neither option stops an explicit import "lodash" from finding @types/lodash; they only control which global declarations are loaded without an import.
Typing a package that has no types
If there is no @types package, declare the module yourself in a .d.ts file that your tsconfig.json includes.
// types/legacy-charts.d.ts
declare module "legacy-charts" {
export interface ChartOptions {
width: number;
height: number;
}
export function render(el: HTMLElement, options: ChartOptions): void;
}
// The shorthand: every import from it is typed any
declare module "tiny-untyped-lib";Type only the functions you use. The shorthand silences the error but turns off checking for that package entirely.
Typing asset imports
Bundlers let you import files that are not JavaScript. TypeScript needs a wildcard module declaration to accept them.
// assets.d.ts
declare module "*.svg" {
const src: string;
export default src;
}
declare module "*.module.css" {
const classes: Readonly<Record<string, string>>;
export default classes;
}The declaration must match what your bundler returns. Vite and Next.js ship these declarations already, which is what vite/client and the generated next-env.d.ts provide.
Global augmentation
To add a property to window or another global type, merge into its interface. Inside a module (a file with any import or export), wrap the declaration in declare global.
// globals.d.ts
export {}; // makes this file a module
declare global {
interface Window {
dataLayer: unknown[];
}
const __APP_VERSION__: string; // replaced by the bundler at build time
}This works because interfaces with the same name merge, which is one of the differences covered in interface vs type. In a file with no imports or exports, top-level declarations are already global, and declare global is an error.
Module augmentation
To add to a library's own types, declare the module by its import path in a file that is itself a module. You can add members to its existing interfaces, but you cannot add new top-level exports.
// i18n.d.ts
import "vue";
declare module "vue" {
interface ComponentCustomProperties {
$t: (key: string) => string;
}
}Many libraries expose interfaces for this purpose, so that your app can register its own error type, theme or translation keys.
Triple-slash directives
A triple-slash directive is a single-line comment at the very top of a file, before any statement.
/// <reference types="vite/client" />
/// <reference path="./legacy-globals.d.ts" />
/// <reference lib="webworker" />typespulls in a package's declarations, like adding it to thetypesoption.pathincludes another file in the compilation, which dates from before modules, when files were concatenated.libadds a built-in library such asdomorwebworker.
Today, import statements express dependencies between your files, and tsconfig.json (include, types, lib) controls what is in the program. Triple-slash directives remain in generated files such as vite-env.d.ts and next-env.d.ts, and in published .d.ts files that depend on another package's globals. In your own code, prefer imports and tsconfig.json.
Practise it
Declaration files are a core step in planning an incremental JavaScript to TypeScript migration. How TypeScript finds a package's types, including the types condition, is part of implementing bundler module resolution.
Common follow-up questions
Why does TypeScript ignore my .d.ts file?
The file is not part of the program. Check that include in tsconfig.json covers it. Also check that a global declaration file has no top-level import or export, or that it uses declare global if it does.
What does skipLibCheck do?
It skips type-checking inside every .d.ts file, including your own. Your code is still checked against them. It speeds up builds and hides conflicts between third-party declarations, which is why most projects turn it on.
Should a library ship its own types or rely on @types?
Ship them. Types generated from the source stay in sync with each release, while @types packages are maintained separately and can lag behind.
Is a .d.ts file the same as a .ts file with only types?
Almost. A .d.ts file never produces output and must not contain implementations. A .ts file with only types still produces a JavaScript file when compiled, usually an empty one. See why use TypeScript for how types are erased.
Mistakes to avoid
Adding
declare module "x";and forgetting that every import from it is nowanyAdding an
importto a global declaration file withoutdeclare global, which turns its globals into module-scoped declarationsDeclaring asset types that do not match the bundler, such as a default export the bundler does not produce
Using
/// <reference path>between your own source files instead of importsSetting
"types": []and then wondering why test globals orprocessstop resolving
Sources
Practise it in code
Interviewers rarely stop at the definition. These questions open in an editor with tests.