TypeScript Guide

Full type safety with generics.

Typed Groups

Define your group names as a union type and pass it as a generic parameter:

type Groups = "api" | "db" | "auth";

const logger = new ZTracer<Groups>({
  global: { debug: true },
  groups: {
    api: { background: "#0d6efd", color: "#fff" },
    db:  { background: "#198754", color: "#fff" },
    auth:{ background: "#dc3545", color: "#fff" }
  }
});

// ✅ TypeScript validates this
await logger.log("Login", { group: "auth" });

// ❌ TypeScript error — "admin" not in Groups
await logger.log("Admin", { group: "admin" });

This catches invalid group names at compile time, not runtime.

Type Inference

TypeScript can infer the group type from your configuration automatically:

// No explicit generic — TypeScript infers Groups = "api" | "db"
const logger = new ZTracer({
  groups: {
    api: { background: "#0d6efd" },
    db:  { background: "#198754" }
  }
});

// ✅ "api" is inferred as valid
await logger.log("Hello", { group: "api" });

For dynamically generated groups, you may need to specify the generic explicitly.

Available Types

All types are exported for use in your own code:

import ZTracer, {
  ZTracerConfig,
  ZTracerOptions,
  ZTracerCallBack,
  ZTracerGroup,
  ZTracerGroups
} from "z-tracer-kit";

// Use them to type your own configurations
type MyGroups = "api" | "db";

const config: ZTracerConfig<MyGroups> = {
  global: { debug: true },
  groups: {
    api: { background: "#0d6efd" },
    db:  { background: "#198754" }
  }
};

Complete Example

import ZTracer, { ZTracerConfig, ZTracerOptions } from "z-tracer-kit";

type LogGroup = "api" | "db" | "system";

const config: ZTracerConfig<LogGroup> = {
  global: {
    debug: process.env.NODE_ENV === "development",
    callBack: async (data: unknown, group?: LogGroup) => {
      // group is typed as LogGroup | undefined
      if (group === "system") {
        console.warn(`[System] ${data}`);
      }
    }
  },
  groups: {
    api: { background: "#0d6efd", color: "#fff" },
    db:  { background: "#198754", color: "#fff" },
    system: { background: "#ffc107", color: "#000" }
  }
};

const logger = new ZTracer(config);

// Type-safe usage with autocomplete in your IDE
await logger.log("API call", { group: "api" });
await logger.log("DB query", { group: "db" });
await logger.log("System health", { group: "system" });
Next: See how ZTracer handles styling in different environments in Runtime & Styling.