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
declaresays the thing exists elsewhere.declare moduletypes an untyped dependency.- A file with no import or export is a script, so these are ambient.
Keywords and builtins used here
OptionsPromisebooleanconstdeclaredefaultexportfromfunctionimportinterfacenumberreturnstringtype
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.
Step 2 of 3 in Modules & declarations, step 6 of 8 in Async & modules.