Sub topic 10
Ambient Declarations for Untyped Packages and Asset Imports
Assume you know basic TypeScript declarations, modules, tsc, and tsconfig.json. This chapter focuses on code that exists at runtime but has no usable type information.
Why ambient declarations exist
An ambient declaration describes something without implementing it. The declare keyword tells TypeScript, “This value exists elsewhere. Check how it is used, but do not emit JavaScript for this declaration.”
This is useful when:
- A JavaScript package has no types.
- A package has incomplete or incorrect types.
- A bundler imports files such as SVG or CSS.
- A runtime supplies a global value that TypeScript cannot see.
A declaration must match reality. TypeScript trusts it, so an incorrect declaration can move an error from compile time to runtime.
// legacy-lib.d.ts
declare module "legacy-lib" {
export function greet(name: string): string;
export const version: string;
}This declaration says that importing legacy-lib provides a greet function and a version value. It does not create the package or its implementation.
// app.ts
import { greet, version } from "legacy-lib";
console.log(greet("Ada"));
console.log(version);The real legacy-lib package must still be installed and must actually provide these exports.
Where TypeScript finds declarations
TypeScript includes declaration files according to your project configuration. A local .d.ts file is usually included through the include pattern in tsconfig.json.
{
"compilerOptions": {
"strict": true,
"module": "nodenext",
"moduleResolution": "nodenext"
},
"include": ["src", "types"]
}With this configuration, a file such as types/legacy-lib.d.ts participates in type checking.
A declaration for a package uses the package's exact specifier. A declaration for "legacy-lib" does not describe "legacy-lib/utils".
Declaring asset imports
Bundlers often transform assets into values that JavaScript can import. TypeScript does not automatically know what an imported SVG or CSS file means. A wildcard ambient module declaration describes every matching path.
// types/assets.d.ts
declare module "*.svg" {
const url: string;
export default url;
}
declare module "*.css" {
const classes: Record<string, string>;
export default classes;
}A bundler might turn an SVG into a URL and a CSS module into an object of class names. The declarations model those bundler results.
// src/logo.ts
import logoUrl from "./logo.svg";
import styles from "./card.css";
const image = document.createElement("img");
image.src = logoUrl;
document.body.className = styles.card;
document.body.append(image);The declaration does not configure the bundler. Configure the bundler separately, and make the declaration agree with its behavior.
Script declarations and module declarations
A .d.ts file containing export or import is an external module. A file with neither can contribute global declarations.
Prefer module-scoped declarations for packages and assets because they avoid accidentally adding names to the whole project. If a declaration file contains helper types or imports, use export {} when you need to make it a module.
// types/runtime.d.ts
export {};
declare global {
interface Window {
appVersion: string;
}
}This describes a property supplied by runtime code without emitting any JavaScript.
Choosing the right solution
| Situation | Usual solution |
|---|---|
| Package already publishes types | Use its bundled declarations |
| Package has types as a separate community package | Install the matching `@types` package |
| Small untyped package or asset pattern | Write a local ambient declaration |
| Existing package types need an added property | Use module augmentation |
Use the narrowest declaration that accurately describes the value. A declaration such as const value: any removes checking and usually postpones the problem.
Exercise
Why does a declaration for "*.svg" need to describe the exported value rather than the SVG file's internal markup?
Show answer
TypeScript checks the value returned by the import. If the bundler returns a URL string, the declaration should export a string.