Declaration files

What .d.ts is

A declaration file holds only types: function signatures, interfaces, and constants with no bodies. Consumers type check against it without ever seeing the implementation.

Think of it as a header for the module. It is small, fast to load, and safe to share with every user of the package.

export function formatDate(value: Date): string; export interface User { name: string; age: number; }

Generate with declaration

Libraries rarely write these files by hand. The compiler generates them from the sources during the build, so types and code can never drift apart.

Turn the flag on once and every build emits JavaScript plus matching type files side by side.

tsc --declaration

The build prints nothing when the types are clean, and the folder now holds app.js with app.d.ts beside it.

Hand-written ambients

Plain JavaScript libraries have no types to generate. A hand-written ambient file describes the global or module shape so TypeScript can check calls into it.

Declare only what you use. A ten line ambient for two functions beats a full rewrite that nobody will maintain.

declare function formatPrice(cents: number): string; declare const APP_VERSION: string;

DefinitelyTyped concept

Popular JavaScript packages share community maintained declarations in one large public collection. When a library ships no types, the collection often has them already.

The idea is simple: one place for widely reused ambient types, reviewed in the open. Your project then type checks third party calls like first party code.

import { formatPrice } from "payments"; const label = formatPrice(1999); console.log(label);

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does a .d.ts file hold?

    Answer

    Types and signatures only, with no implementation bodies

    The declaration file is a header: consumers check against it without seeing the code.

  2. What does tsc --declaration add to a build?

    Answer

    A .d.ts file beside every emitted JavaScript file

    The compiler writes the types next to the output, so the two cannot drift apart.

  3. What is an ambient declaration for?

    Answer

    Describing the shape of JavaScript code that has no TypeScript types

    A declare statement tells the checker about globals or modules it cannot read.

  4. What is DefinitelyTyped?

    Answer

    A community collection of declarations for JavaScript packages

    The collection holds widely used ambient types, reviewed in the open.

  5. What does this program print? console.log(new Date(0).toISOString());

    Answer

    1970-01-01T00:00:00.000Z

    A zero millisecond date is the start of 1970 in UTC, and toISOString prints it.

  6. What does this program print? console.log("ada".toUpperCase());

    Answer

    ADA

    toUpperCase rewrites every letter to upper case.

  7. What does this program print? console.log((1999 / 100).toFixed(2));

    Answer

    19.99

    The division gives 19.99, and toFixed keeps two digits after the dot.

  8. What does this program print? console.log(typeof new Date());

    Answer

    object

    Every object, including a Date, reports object to typeof.

  9. What does the compiler say about this program? declare function formatPrice(cents: number): string { return "$" + cents; } console.log(formatPrice(1999));

    Answer

    error TS1183: An implementation cannot be declared in ambient contexts.

    A declare statement describes a shape and cannot carry a body.

  10. What does the compiler say about this program? declare function formatPrice(cents: number): string; function formatPrice(cents: number): string { return "$" + cents; } console.log(formatPrice(1999));

    Answer

    error TS2384: Overload signatures must all be ambient or non-ambient.

    One signature is ambient and the other has a body, and TypeScript refuses to mix them.

Article author: Arthur Isaev