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
