
Security News
TypeScript 6.0 Released: The Final JavaScript-Based Version
TypeScript 6.0 introduces new standard APIs, modern default settings, and deprecations as it prepares projects for the upcoming TypeScript 7.0 release.
@xylabs/assert
Advanced tools
Base functionality used throughout XY Labs TypeScript/JavaScript libraries
Base functionality used throughout XY Labs TypeScript/JavaScript libraries
@xylabs/assert
| Function | Description |
|---|---|
| assertDefinedEx | Implementation of assertDefinedEx that handles all overloads. |
| assertEx | Implementation of assertEx that handles all overloads. |
Implementation of assertDefinedEx that handles all overloads.
function assertDefinedEx<T>(expr: T | undefined, messageFunc?: AssertExMessageFunc<T>): T;
Asserts that a value is defined (not undefined) and returns the value. Throws an error if the value is undefined.
| Type Parameter | Description |
|---|---|
T | The type of value to check |
| Parameter | Type | Description |
|---|---|---|
expr | T | undefined | Expression to be evaluated for being defined |
messageFunc? | 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: T | undefined, errorFunc?: AssertExErrorFunc<T, R>): T;
Asserts that a value is defined (not undefined) and returns the value. Throws a custom error if the value is undefined.
| Type Parameter | Description |
|---|---|
T | The type of value to check |
R extends Error | The type of error to throw |
| Parameter | Type | Description |
|---|---|---|
expr | T | undefined | Expression to be evaluated for being defined |
errorFunc? | 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'))
Implementation of assertEx that handles all overloads.
function assertEx<T>(expr: T | null | undefined, messageFunc?: AssertExMessageFunc<T>): T;
Asserts that an expression is truthy and returns the value. Throws an error if the expression is falsy.
| Type Parameter | Description |
|---|---|
T | The type of value to check |
| Parameter | Type | Description |
|---|---|---|
expr | T | null | undefined | Expression to be evaluated for truthiness |
messageFunc? | 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: T | null | undefined, errorFunc?: AssertExErrorFunc<T, R>): T;
Asserts that an expression is truthy and returns the value. Throws a custom error if the expression is falsy.
| Type Parameter | Description |
|---|---|
T | The type of value to check |
R extends Error | The type of error to throw |
| Parameter | Type | Description |
|---|---|---|
expr | T | null | undefined | Expression to be evaluated for truthiness |
errorFunc? | 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'))
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 1,240 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.

Security News
TypeScript 6.0 introduces new standard APIs, modern default settings, and deprecations as it prepares projects for the upcoming TypeScript 7.0 release.

Security News
/Research
Newly published Trivy Docker images (0.69.4, 0.69.5, and 0.69.6) were found to contain infostealer IOCs and were pushed to Docker Hub without corresponding GitHub releases.

Research
/Security News
The worm-enabled campaign hit @emilgroup and @teale.io, then used an ICP canister to deliver follow-on payloads.