
Product
Introducing License Overlays: Smarter License Management for Real-World Code
Customize license detection with Socket’s new license overlays: gain control, reduce noise, and handle edge cases with precision.
@xylabs/assert
Advanced tools
Base functionality used throughout XY Labs TypeScript/JavaScript libraries
Base functionality used throughout XY Labs TypeScript/JavaScript libraries
@xylabs/assert
Implementation of assertDefinedEx that handles all overloads.
function assertDefinedEx<T>(expr, messageFunc?): T;
Asserts that a value is defined (not undefined) and returns the value. Throws an error if the value is undefined.
T
The type of value to check
Expression to be evaluated for being defined
undefined
| T
AssertExMessageFunc
<T
>
Function that returns a message for the error if expression is undefined
T
The value of the expression (guaranteed to be defined)
Error with the message returned by messageFunc
// Simple usage with a message function
const value = assertDefinedEx(possiblyUndefined, () => 'Value must be defined')
// Using with type narrowing
const config: Config | undefined = loadConfig()
const safeConfig = assertDefinedEx(config, () => 'Config failed to load')
// safeConfig is now type Config (not Config | undefined)
function assertDefinedEx<T, R>(expr, errorFunc?): T;
Asserts that a value is defined (not undefined) and returns the value. Throws a custom error if the value is undefined.
T
The type of value to check
R
extends Error
The type of error to throw
Expression to be evaluated for being defined
undefined
| T
AssertExErrorFunc
<T
, R
>
Function that returns a custom error instance if expression is undefined
T
The value of the expression (guaranteed to be defined)
Custom error returned by errorFunc
// Using with a custom error
const user = assertDefinedEx(getUser(), () => new UserNotFoundError('User not found'))
function assertDefinedEx<T>(expr): T;
Asserts that a value is defined (not undefined) and returns the value. Throws an error if the value is undefined.
T
The type of value to check
Expression to be evaluated for being defined
undefined
| T
T
The value of the expression (guaranteed to be defined)
Use overload with message function instead - passing a message will soon be required
Error with a generic message
function assertDefinedEx<T>(expr, message?): T;
Asserts that a value is defined (not undefined) and returns the value. Throws an error with the provided message if the value is undefined.
T
The type of value to check
Expression to be evaluated for being defined
undefined
| T
string
Error message if expression is undefined
T
The value of the expression (guaranteed to be defined)
Replace string with () => string for consistency
Error with the provided message
Implementation of assertEx that handles all overloads.
function assertEx<T>(expr, messageFunc?): T;
Asserts that an expression is truthy and returns the value. Throws an error if the expression is falsy.
T
The type of value to check
Expression to be evaluated for truthiness
undefined
| null
| T
AssertExMessageFunc
<T
>
Function that returns a message for the error if expression is falsy
T
The value of the expression (guaranteed to be truthy)
Error with the message returned by messageFunc
// Simple usage with a message function
const value = assertEx(possiblyFalsy, () => 'Value must be truthy')
// Using with type narrowing
const config: Config | null = loadConfig()
const safeConfig = assertEx(config, () => 'Config failed to load')
// safeConfig is now type Config (not Config | null)
function assertEx<T, R>(expr, errorFunc?): T;
Asserts that an expression is truthy and returns the value. Throws a custom error if the expression is falsy.
T
The type of value to check
R
extends Error
The type of error to throw
Expression to be evaluated for truthiness
undefined
| null
| T
AssertExErrorFunc
<T
, R
>
Function that returns a custom error instance if expression is falsy
T
The value of the expression (guaranteed to be truthy)
Custom error returned by errorFunc
// Using with a custom error
const user = assertEx(getUser(), () => new UserNotFoundError('User not found'))
function assertEx<T>(expr): T;
Asserts that an expression is truthy and returns the value. Throws an error if the expression is falsy.
T
The type of value to check
Expression to be evaluated for truthiness
undefined
| null
| T
T
The value of the expression (guaranteed to be truthy)
Use overload with message function instead - passing a message will soon be required
Error with a generic message
function assertEx<T>(expr, message?): T;
Asserts that an expression is truthy and returns the value. Throws an error with the provided message if the expression is falsy.
T
The type of value to check
Expression to be evaluated for truthiness
undefined
| null
| T
string
Error message if expression is falsy
T
The value of the expression (guaranteed to be truthy)
Replace string with () => string for consistency
Error with the provided message
Part of sdk-js
See the LICENSE file for license details
FAQs
Base functionality used throughout XY Labs TypeScript/JavaScript libraries
The npm package @xylabs/assert receives a total of 11,774 weekly downloads. As such, @xylabs/assert popularity was classified as popular.
We found that @xylabs/assert demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Customize license detection with Socket’s new license overlays: gain control, reduce noise, and handle edge cases with precision.
Product
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.
Product
Socket’s precomputed reachability slashes false positives by flagging up to 80% of vulnerabilities as irrelevant, with no setup and instant results.