| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Runtime guards and type-safe results for TypeScript and JavaScript.
GuardX helps you validate values at runtime while preserving TypeScript's type narrowing. Use assertion functions when invalid data should stop execution, type guards when you want to handle it yourself, or result objects when you want to avoid repetitive try/catch blocks.
npm install guardxpnpm add guardxyarn add guardxAssertions throw at runtime and narrow the value for all code that follows.
import * as assert from 'guardx/assert';
type User = {
id: number;
emailAddress: string;
};
declare function findUserById(userId: number): User | undefined;
const user = findUserById(123);
assert.isDefined(user, 'User not found');
// TypeScript now knows that user is a User.
user.emailAddress = 'new-email@example.com';You can pass either an error message or an Error instance:
assert.isDefined(user, new Error('User not found'));Checks return booleans and leave the response to your application.
import * as check from 'guardx/check';
declare const value: string | null | undefined;
if (check.isNullOrUndefined(value)) {
console.log('No value was provided');
} else {
// value is narrowed to string.
console.log(value.toUpperCase());
}safe and safeAsync return a discriminated union, so checking success narrows the result to either its output or error.
import * as run from 'guardx/run';
const result = run.safe(() => JSON.parse('{"ready":true}'));
if (result.success) {
console.log(result.output);
} else {
console.error(result.error);
}For promises, use safeAsync:
const result = await run.safeAsync(() => fetch('/api/users/123'));
if (!result.success) {
console.error('Request failed', result.error);
}You can import every module from the package root:
import { assert, check, run, util } from 'guardx';Or import only the module you need:
import * as assert from 'guardx/assert';
import * as check from 'guardx/check';
import * as run from 'guardx/run';
import * as util from 'guardx/util';| Module | Purpose | Available functions |
|---|---|---|
| assert | Validate a value or throw | isDefined, isNotNullOrUndefined, isNotNull, isNotUndefined, isUndefined, isEqual, isNotEqual, isOneOf, isTrue, isFalse, isBoolean, isString, isNumber, isBigInt, isSymbol, isFunction, isObject, isArray |
| check | Narrow values with type guards | isDefined, isNullOrUndefined, isNull, isUndefined, isString, isBoolean, isNumber, isBigInt, isSymbol, isFunction, isObject |
| run | Capture function output or errors | safe, safeAsync |
| util | Shared utility helpers | defaultTo, fail |
See the generated documentation for additional API details.
GuardX uses pnpm, Nx, and Vitest. Node.js 20 is used in CI.
pnpm install
pnpm exec nx test guardx
pnpm exec nx lint guardx
pnpm exec nx build guardxTo generate the API documentation locally:
pnpm exec nx docs guardxBug reports and pull requests are welcome. Before opening a pull request, please run the test, lint, and build commands above.
GuardX is available under the MIT License.
| Back | FazBrowse Home | New Git URL |