typestar

Declaration files in TypeScript

A .d.ts describes shapes with no implementation.

declare module "legacy-lib" {
  export interface Options {
    retries?: number;
  }
  export function fetchAll(
    urls: string[],
    options?: Options,
  ): Promise<string[]>;
  const version: string;
  export default version;
}

declare module "legacy-lib/plugin" {
  import type { Options } from "legacy-lib";
  export function register(options: Options): void;
}

declare const __DEV__: boolean;

function whichBuild(): string {
  return __DEV__ ? "development" : "production";
}

console.log(whichBuild.length);

How it works

  1. declare says the thing exists elsewhere.
  2. declare module types an untyped dependency.
  3. A file with no import or export is a script, so these are ambient.

Keywords and builtins used here

The run, in numbers

Lines
24
Characters to type
489
Tokens
100
Three-star pace
110 tpm

At the three-star pace of 110 tokens a minute, this run takes about 55 seconds.

Type this snippet

Step 2 of 3 in Modules & declarations, step 6 of 8 in Async & modules.

← Previous Next →