fn-utils

Version 2.1 by Pierre Jeanjean on 2025/10/20 16:18

Description

Fn-Utils provides several function utilities and types to simplify some common operations.

API

/**
 * Ensure a statement is unreachable
 *
 * @param value - the error value printed when this method is reached
 *
 * @since 0.17
 */
function assertUnreachable(value: never): never;

/**
 * Assert that a value is in an array, and fix its type
 *
 * @since 0.17
 *
 * @param array - the array to check
 * @param value - the value expected in the array
 * @param message - a message displayed in case the value is not found in the array
 *
 * @returns Whether the value is in the provided array, with the correct type
 */
function assertInArray<T, U extends T>(value: T, array: U[], message: string): U;

/**
 * Get a function's output or `null` if thrown an error
 *
 * @since 0.17
 *
 * @param func - The function to try
 *
 * @returns The function's output, or `null` if it thrown an error
 */
function tryFallible<T>(func: () => T): T | null;

/**
 * Get a function's output of the thrown error
 * Will construct a new Error object if the thrown value is not an instance of the Error class
 *
 * @since 0.17
 *
 * @param func - The function to try
 *
 * @returns The function's output, the thrown error if it's an instance of the `Error` class, or a constructed `Error` instance
 */
function tryFallibleOrError<T>(func: () => T): T | Error;

/**
 * Generic tree structure type.
 * @since 0.23
 * @beta
 */
type TreeNode<T> = T & {
  children?: TreeNode<T>[];
};

Get Connected