Sub topic 11
Declaration Files and `@types` Packages
Assume you already know JavaScript modules, npm packages, and the basics of tsc. This chapter explains how TypeScript understands JavaScript code that has no type annotations.
Why declaration files exist
A declaration file is a file ending in .d.ts. It describes the types that code provides, but it contains no executable implementation. TypeScript reads declarations during type checking and leaves them out of JavaScript output.
A JavaScript package might export a function like this:
export function slugify(text) {
return text.toLowerCase().replaceAll(" ", "-");
}TypeScript cannot reliably infer the intended public contract from every JavaScript package. A declaration file can describe that contract:
export declare function slugify(text: string): string;The declare keyword says that the implementation exists elsewhere. The declaration only tells the compiler how to check calls to the function.
Consuming a package's declarations
A package can publish its own declarations. TypeScript commonly finds them through the types or typings field in that package's package.json:
{
"name": "text-tools",
"main": "dist/index.js",
"types": "dist/index.d.ts"
}When your code imports text-tools, TypeScript reads dist/index.d.ts to learn the available exports.
| Package contents | TypeScript behavior |
|---|---|
| JavaScript and bundled `.d.ts` files | Uses the package's declarations |
| JavaScript without declarations | May report missing type declarations |
| TypeScript source published with declarations | Uses the generated public types |
The declaration must match the runtime API. If it says a function returns a number but the JavaScript returns a string, the compiler accepts incorrect assumptions.
Using `@types` packages
Many older or JavaScript-only packages do not publish declarations. The community maintains declarations for many of them in packages named @types/<package-name>.
Install a declaration package as a development dependency:
npm install lodash
npm install --save-dev @types/lodashThe lodash package is needed when the program runs. The @types/lodash package is needed while TypeScript checks the source. The declaration package normally does not add a second runtime copy of the library.
For example:
import { chunk } from "lodash";
const groups: number[][] = chunk([1, 2, 3, 4], 2);
console.log(groups);TypeScript uses the declarations to check the import and infer that groups is an array of number arrays.
The package name is not always an exact match. Some packages include their own types, and some have no maintained @types package. Check the package documentation or npm metadata before installing one.
Writing a local declaration
You can describe an untyped module in your project. Create a file such as src/vendor.d.ts:
declare module "legacy-format" {
export function formatDate(value: Date, pattern: string): string;
}This declaration allows an import:
import { formatDate } from "legacy-format";
const label = formatDate(new Date(), "yyyy-MM-dd");Your tsconfig.json must include the directory containing the declaration file. A declaration file under an included src directory is usually found automatically.
Use a local declaration as a temporary boundary. Keep it accurate and narrow. Avoid declaring every value as any, because that removes the checks the declaration was meant to provide.
Generating declarations for your own library
If you publish TypeScript code, ask tsc to generate declarations:
{
"compilerOptions": {
"declaration": true,
"emitDeclarationOnly": true,
"outDir": "dist"
},
"include": ["src"]
}For a source file such as src/index.ts, the compiler can produce dist/index.d.ts. Consumers then receive the public function signatures without receiving your source implementation.
Exercise
Exercise
Why is @types/lodash a dev dependency while lodash is a runtime dependency?
Show answer
TypeScript reads @types/lodash while checking source code, but the running program imports and executes lodash.