Dmytro Morar
TypeScript

d.ts files

Declaration files describe types for code that exists at runtime but is not written in TS. They are used for type checking only and emit no JS.

Basics

// globals.d.ts
declare const VERSION: string;
declare function logMessage(msg: string): void;
// main.ts
console.log(VERSION);
logMessage("Hello!");

External libraries

  • Install community types when available.
npm i --save-dev @types/lodash
  • Or write your own declarations.
declare module "lodash" {
  export function chunk<T>(arr: T[], size?: number): T[][];
}

Notes

  • Use declare for external entities without implementations.
  • Common for globals, untyped packages, and shared type packages.

On this page