Skip to main content
Home

Built and signed on GitHub Actions

Works with
This package works with Node.js, DenoIt is unknown whether this package works with Cloudflare Workers, Bun, Browsers
It is unknown whether this package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
It is unknown whether this package works with Bun
It is unknown whether this package works with Browsers
JSR Score100%
Downloads5/wk
Published2 days ago (0.2.0)

Type-safe runtime assertions with definable error factories and TypeScript narrowing

Type-safe runtime assertions with definable error factories and TypeScript narrowing.

Examples

Basic usage

import { ensure } from "@zerocity/define-ensure";

// Returns the value, narrowed by TypeScript
const user = ensure(maybeUser, "User is required");
user.name; // ✅ TypeScript knows user is defined

Custom validators with defineEnsure

import { defineEnsure } from "@zerocity/define-ensure";

class ValidationError extends Error {
  override name = "ValidationError";
}

const [validate, isValidationError] = defineEnsure({
  error: ValidationError,
});

const email = validate(formData.email, "Email required");
Built and signed on
GitHub Actions

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@zerocity/define-ensure

Import symbol

import * as define_ensure from "@zerocity/define-ensure";
or

Import directly with a jsr specifier

import * as define_ensure from "jsr:@zerocity/define-ensure";

Add Package

pnpm i jsr:@zerocity/define-ensure
or (using pnpm 10.8 or older)
pnpm dlx jsr add @zerocity/define-ensure

Import symbol

import * as define_ensure from "@zerocity/define-ensure";

Add Package

yarn add jsr:@zerocity/define-ensure
or (using Yarn 4.8 or older)
yarn dlx jsr add @zerocity/define-ensure

Import symbol

import * as define_ensure from "@zerocity/define-ensure";

Add Package

vlt install jsr:@zerocity/define-ensure

Import symbol

import * as define_ensure from "@zerocity/define-ensure";

Add Package

npx jsr add @zerocity/define-ensure

Import symbol

import * as define_ensure from "@zerocity/define-ensure";

Add Package

bunx jsr add @zerocity/define-ensure

Import symbol

import * as define_ensure from "@zerocity/define-ensure";