or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

dom-events.mddom-utilities.mdfocus-accessibility.mdfunction-utilities.mdindex.mdmath-utilities.mdobject-manipulation.mdreact-utilities.mdresponsive.mdtype-checking.md
tile.json

type-checking.mddocs/

Type Checking Utilities

Comprehensive collection of type guard functions and validation utilities for JavaScript values. These functions provide runtime type checking with TypeScript type narrowing support, essential for handling dynamic data and ensuring type safety in React applications.

Capabilities

String Type Checking

Type guards and utilities for string validation.

/**
 * Type guard that checks if a value is a string
 * @param value - Value to check
 * @returns true if value is a string, with type narrowing
 */
function isString(value: any): value is string;

/**
 * Checks if a string represents a CSS custom property (CSS variable)
 * @param value - String to check
 * @returns true if string matches var(--*) format
 */
function isCssVar(value: string): boolean;

Number Type Checking

Type guards and utilities for numeric validation.

/**
 * Type guard that checks if a value is a number
 * @param value - Value to check
 * @returns true if value is a number (excluding NaN), with type narrowing
 */
function isNumber(value: any): value is number;

/**
 * Checks if a value is not a number
 * @param value - Value to check
 * @returns true if value is not a valid number
 */
function isNotNumber(value: any): boolean;

/**
 * Checks if a value is numeric (number or numeric string)
 * @param value - Value to check
 * @returns true if value can be converted to a valid number
 */
function isNumeric(value: any): boolean;

Usage Examples:

import { isNumber, isNumeric, isNotNumber } from "@chakra-ui/utils";

// Type guard usage
function processValue(input: unknown) {
  if (isNumber(input)) {
    // TypeScript knows input is number here
    return input.toFixed(2);
  }
  return "Not a number";
}

// Numeric validation
const userInput = "42.5";
if (isNumeric(userInput)) {
  const num = parseFloat(userInput); // Safe to parse
}

// Filtering non-numbers
const mixedArray = [1, "hello", 2, null, 3];
const numbers = mixedArray.filter(item => !isNotNumber(item));

Object Type Checking

Type guards and utilities for object validation.

/**
 * Type guard that checks if a value is a plain object
 * @param value - Value to check
 * @returns true if value is a plain object, with type narrowing
 */
function isObject(value: any): value is Record<string, any>;

/**
 * Checks if an object has no enumerable properties
 * @param value - Value to check
 * @returns true if object is empty or not an object
 */
function isEmptyObject(value: any): boolean;

Array Type Checking

Type guards and utilities for array validation.

/**
 * Type guard that checks if a value is an array
 * @param value - Value to check
 * @returns true if value is an array, with type narrowing
 */
function isArray<T>(value: any): value is Array<T>;

/**
 * Checks if an array has no elements
 * @param value - Value to check
 * @returns true if array is empty or not an array
 */
function isEmptyArray(value: any): boolean;

Function Type Checking

Type guard for function validation.

/**
 * Type guard that checks if a value is a function
 * @param value - Value to check
 * @returns true if value is a function, with type narrowing
 */
function isFunction<T extends Function>(value: any): value is T;

Null and Undefined Checking

Type guards for null and undefined values.

/**
 * Type guard that checks if a value is null or undefined
 * @param value - Value to check
 * @returns true if value is null or undefined, with type narrowing
 */
function isNull(value: any): value is null;

/**
 * Type guard that checks if a value is undefined
 * @param value - Value to check
 * @returns true if value is undefined, with type narrowing
 */
function isUndefined(value: any): value is undefined;

/**
 * Checks if a value is defined (not null or undefined)
 * @param value - Value to check
 * @returns true if value is not null and not undefined
 */
function isDefined(value: any): boolean;

General Emptiness Checking

Utility for checking if values are considered "empty".

/**
 * Checks if a value is considered empty
 * Empty values include: null, undefined, empty string, empty array, empty object
 * @param value - Value to check
 * @returns true if value is considered empty
 */
function isEmpty(value: any): boolean;

React-Specific Type Checking

Type guards for React-specific objects.

/**
 * Type guard that checks if a value is a React ref object
 * @param val - Value to check
 * @returns true if value has a 'current' property
 */
function isRefObject(val: any): val is { current: any };

/**
 * Type guard that checks if a value is an input event
 * @param value - Value to check
 * @returns true if value is an event with HTMLInputElement target
 */
function isInputEvent(value: any): value is { target: HTMLInputElement };

Usage Examples:

import { 
  isObject, isArray, isEmpty, 
  isRefObject, isInputEvent 
} from "@chakra-ui/utils";

// Object validation
function processConfig(config: unknown) {
  if (isObject(config)) {
    // TypeScript knows config is Record<string, any>
    return config.setting || "default";
  }
}

// Array validation
function processItems(items: unknown) {
  if (isArray<string>(items)) {
    // TypeScript knows items is string[]
    return items.map(item => item.toUpperCase());
  }
}

// Emptiness checking
function validateInput(input: unknown): string | null {
  if (isEmpty(input)) {
    return null;
  }
  return String(input);
}

// React ref checking
function focusElement(ref: unknown) {
  if (isRefObject(ref) && ref.current?.focus) {
    ref.current.focus();
  }
}

// Event handling
function handleChange(event: unknown) {
  if (isInputEvent(event)) {
    // TypeScript knows event.target is HTMLInputElement
    console.log(event.target.value);
  }
}