@leancodepl/utils
Version:
Common utility functions and React hooks for web applications
19 lines (18 loc) • 737 B
TypeScript
/**
* Ensures that a value is not null, returning it if not null or throwing an error if null.
* Unlike assertNotNull, this function returns the value for use in expressions.
*
* @template T - The type of the value being checked
* @param value - The value to ensure is not null
* @param message - Optional error message to use if the value is null
* @returns The value if it is not null
* @throws {Error} When the value is null
* @example
* ```typescript
* function processData(data: string | null) {
* const nonNullData = ensureNotNull(data);
* return nonNullData.toUpperCase(); // nonNullData is guaranteed to be not null
* }
* ```
*/
export declare function ensureNotNull<T>(value: T | null, message?: string): T;