
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
assert-or-return
Advanced tools
Type-safe assertion library with decorator-based early returns for TypeScript
Type-safe assertion library with decorator-based early returns for TypeScript.
npm install assert-or-return
import { assert, HasAssertions, AssertResult, Ok, Err } from 'assert-or-return';
class Calculator {
// With assert-or-return decorator (simplified)
@HasAssertions()
static add(a?: number, b?: number): AssertResult<number> {
assert(a, "Parameter 'a' is required");
assert(b, "Parameter 'b' is required");
return Ok(a + b);
}
// Without decorator (manual error handling)
static addWithoutAssertions(a?: number, b?: number): AssertResult<number> {
if (!a) {
return Err("Parameter 'a' is required");
}
if (!b) {
return Err("Parameter 'b' is required");
}
return Ok(a + b);
}
}
// Usage
const result1 = Calculator.add(1, 2);
if (result1.success) {
console.log(result1.data); // 3
} else {
console.log(result1.error); // Type-safe error handling
}
const result2 = Calculator.add(1); // Missing second parameter
console.log(result2); // { success: false, error: "Parameter 'b' is required" }
The decorator approach eliminates the need for manual if checks and return Err() statements, making your code more concise and readable.
assert(value, errorMessage) - Throws an assertion error if value is falsy@HasAssertions() - Decorator that catches assertion errors and returns them as AssertResultAssertResult<T> - Type-safe result type: { success: true, data: T } or { success: false, error: string }Ok(value) - Helper to create success resultsThe library provides full TypeScript support:
// TypeScript knows the types after assertions
function processUser(user?: User): AssertResult<string> {
assert(user, "User is required");
assert(user.name, "User name is required");
// TypeScript knows user and user.name are non-null here
return Ok(`Hello ${user.name}!`);
}
"experimentalDecorators": trueAdd these options to your tsconfig.json:
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
assert<T>(value: T, errorMessage: string): asserts value is NonNullable<T>Asserts that a value is truthy. If the assertion fails, throws an error that gets caught by the @HasAssertions() decorator.
@HasAssertions()Class method decorator that catches assertion errors and converts them to AssertResult return values.
AssertResult<T>Type representing either success or failure:
{ success: true, data: T } - Successful result with data{ success: false, error: string } - Failed result with error messageOk<T>(data: T): AssertResult<T>Helper function to create successful results.
Err(error: string): AssertResult<never>Helper function to create error results.
The @HasAssertions() decorator only works on class methods. It cannot be used on:
// ✅ Works - class method
class MyClass {
@HasAssertions()
static myMethod(): AssertResult<string> {
assert(someValue, "Error message");
return Ok("success");
}
}
// ❌ Doesn't work - standalone function
@HasAssertions() // This won't work
function myFunction(): AssertResult<string> {
assert(someValue, "Error message");
return Ok("success");
}
When assert() is used without the @HasAssertions() decorator, it behaves like Node.js's built-in assert module - it simply throws an AssertionError that you must catch yourself:
import { assert } from 'assert-or-return';
function withoutDecorator(value?: string) {
try {
assert(value, "Value is required"); // Throws AssertionError if value is falsy
console.log("Value is:", value);
} catch (error) {
console.log("Caught error:", error.message); // Manual error handling required
}
}
This behavior is consistent with Node.js's node:assert module, making the library familiar to developers already using assertions.
MIT
FAQs
Type-safe assertion library with decorator-based early returns for TypeScript
We found that assert-or-return demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.