TypeScript Declaration Files

Named ambient module declaration

A named ambient module in a script declaration file describes a module that is not otherwise typed. Its presence does not install a runtime implementation. Unlike module augmentation in an external module, this declares the module contract itself. The example is compile-only. Baseline: TypeScript 5.8.3. File labels are explanatory; create the files separately.

Formal Syntax & Structure

typescript syntaxtypescript
declare module "package-name" { /* exports */ }

Working Example

Example Snippettypescript
// File: tsconfig.json (omit this label){  "compilerOptions": {    "target": "ES2022",    "module": "CommonJS",    "strict": true,    "types": [],    "noEmit": true,    "skipLibCheck": false,    "lib": [      "ES5"    ]  },  "files": [    "main.ts"  ]}
// File: vendor.d.ts (omit this label)declare module "virtual-settings" {  export const port: number;}
// File: main.ts (omit this label)/// <reference path="./vendor.d.ts" />import { port } from "virtual-settings";const checked: number = port;
// Run: tsc -p . --pretty false// Expected: no diagnostics.
TypeScript Reference