liveBook · Manning

Debug package

As explained in Essential TypeScript 5, Third Edition

The debug package is a JavaScript utility package that produces decorated debugging output through the console.

The debug package produces decorated debugging output through the JavaScript console. Install version 4.3.4 with npm, then import it into a TypeScript application. section 15.3.3

Listing 15.22 Adding a package to the example project
npm install debug@4.3.4

Install the package in the project with npm. section 15.3.3

Because the package is published as CommonJS and does not provide the needed TypeScript information automatically, add a debug.d.cts declaration and configure tsconfig.json to resolve declarations from the types folder. section 15.3.3

Listing 15.23 Configuring the compiler in the tsconfig.json file in the usingjs folder
{
    "compilerOptions": {
        "target": "ES2022",
        "outDir": "./dist",
        "rootDir": "./src",
        "module": "Node16",
        "allowJs": true,
        "checkJs": true,
        "baseUrl": ".",
        "paths": {
            "*": ["types/*.d.cts", "types/*.d.mts", "types/*.d.ts"]
        },
    }
}

Configure Node16 module resolution, baseUrl, and paths for declaration files. section 15.3.3

Listing 15.24 The contents of the debug.d.cts file in the types folder
declare interface Debug {
   (namespace: string): Debugger
}
declare interface Debugger {
   (...args: string[]): void;
   enabled: boolean;
}

declare var debug: Debug & { default: Debug };

export = debug;

Declare the callable debug function, its enabled property, and the CommonJS export. section 15.3.3

Using a debugger

Import debug, create a debugger with a namespace, enable it, and emit a formatted message: debug("Example App", true); db.enabled = true; db("Message: %s", "Test message"). The shown declaration accepts only one namespace argument, so the two-argument call produces a compiler error. section 15.3.3

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Explains installation, module-resolution configuration, declarations, usage, and an argument mismatch, but not the package implementation.
Examples
Uses npm installation, tsconfig.json paths, debug.d.cts declarations, and an index.ts debugger call.
What it assumes
Assumes JavaScript, npm packages, TypeScript imports, and basic tsconfig.json configuration.
What you can do after
Apply it via the debug package, configure its TypeScript declarations, and diagnose usage errors with the compiler.
How it connects
Connects debugging output with npm packages, TypeScript declaration files, CommonJS modules, and compiler module resolution.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.