Skip to main content

@std/assert@1.0.12
Built and signed on GitHub Actions

Common assertion functions, especially useful for testing

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
2 days ago (1.0.12)
function assertExists
assertExists<T>(
actual: T,
msg?: string,
): asserts actual is NonNullable<T>

Make an assertion that actual is not null or undefined. If not then throw.

Examples

Usage

import { assertExists } from "@std/assert";

assertExists("something"); // Doesn't throw
assertExists(undefined); // Throws

Type Parameters

The type of the actual value.

Parameters

actual: T

The actual value to check.

optional
msg: string

The optional message to include in the error if the assertion fails.

Return Type

asserts actual is NonNullable<T>

Add Package

deno add jsr:@std/assert

Import symbol

import { assertExists } from "@std/assert/exists";

---- OR ----

Import directly with a jsr specifier

import { assertExists } from "jsr:@std/assert/exists";

Add Package

npx jsr add @std/assert

Import symbol

import { assertExists } from "@std/assert/exists";

Add Package

yarn dlx jsr add @std/assert

Import symbol

import { assertExists } from "@std/assert/exists";

Add Package

pnpm dlx jsr add @std/assert

Import symbol

import { assertExists } from "@std/assert/exists";

Add Package

bunx jsr add @std/assert

Import symbol

import { assertExists } from "@std/assert/exists";