TypeScript definitions for the warning library - a mirror of Facebook's warning utility for logging development-time warnings when conditions are not met
npx @tessl/cli install tessl/npm-types--warning@3.0.0TypeScript definitions for the warning library - a mirror of Facebook's warning utility for logging development-time warnings when conditions are not met. The warning library enables developers to conditionally log warnings to help with debugging and development, designed to be stripped from production builds.
npm install @types/warningnpm install warningimport warning = require("warning");For projects with module resolution:
import * as warning from "warning";import warning = require("warning");
// Warning when condition is falsy - logs to console.error
warning(false, "This will show a warning message");
// No warning when condition is truthy
warning(true, "This will not show a warning");
// Warning with formatted message and extra parameters
warning(false, "User %s has invalid age: %d", "Alice", -5);
// Common usage patterns
const isValidUser = user && user.name && user.age > 0;
warning(isValidUser, "Invalid user object provided to UserComponent");
const isProduction = process.env.NODE_ENV === "production";
warning(!isProduction, "Debug mode enabled - performance may be affected");The warning library implements a simple functional architecture pattern:
export = warning)console.error() when falsyConditionally logs warning messages to console.error when a condition is not met. Designed for development-time debugging and should be stripped from production builds using tools like babel-plugin-dev-expression.
/**
* Conditionally logs a warning message when condition is falsy
* @param condition - The condition to check (any falsy value triggers warning)
* @param format - Optional warning message format string
* @param extra - Additional arguments for formatted message
* @returns void
*/
declare const warning: (condition: any, format?: string, ...extra: any[]) => void;Parameters:
condition (any): Condition to evaluate - warning is logged when falsy (false, 0, "", null, undefined, etc.)format (optional string): Warning message or format string for additional parameters...extra (rest parameters, any[]): Additional arguments used with format string for message interpolationBehavior:
console.error() (not console.warn()) when condition is falsyUsage Examples:
// Basic conditional warning
warning(user.isValid, "User validation failed");
// Warning with interpolated values
warning(age >= 18, "Age %d is below minimum required age", age);
// Object validation
warning(
config && typeof config.apiKey === "string",
"Configuration missing required apiKey property"
);
// Development environment checks
warning(
process.env.NODE_ENV !== "production",
"Performance debugging enabled in production environment"
);This warning utility is specifically designed for development-time debugging:
console.error() instead of console.warn() for consistency with Facebook's original implementationThe package uses CommonJS export pattern:
export = warning;This requires the import syntax:
import warning = require("warning");