Skip to main content

Works with
This package works with Bun
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Browsers
JSR Score
100%
Published
3 months ago (0.1.0)

assert

JSR JSR Score

A handful of useful assertions for type narrowing.

Usage

declare const x: number | string | undefined
assertTruthy(x) // number | string

// Also usable with type guarding expressions!
assertTruthy(typeof x === 'string') // Narrows x to string
import { assertIsDefined } from '@knudsen/assert'

const user = await fetchUser()
assertIsDefined(response.profileImage, 'profileImage')

console.log(user.profileImage) // profileImage is now non-nullable
import { ensure } from '@knudsen/assert'

const user = await fetchUser()
const profileImage = ensure(response.profileImage, 'profileImage')

console.log(profileImage) // profileImage is now non-nullable
Built and signed on
GitHub Actions
View transparency log

Add Package

deno add @knudsen/assert

Import symbol

import * as mod from "@knudsen/assert";

Add Package

npx jsr add @knudsen/assert

Import symbol

import * as mod from "@knudsen/assert";

Add Package

yarn dlx jsr add @knudsen/assert

Import symbol

import * as mod from "@knudsen/assert";

Add Package

pnpm dlx jsr add @knudsen/assert

Import symbol

import * as mod from "@knudsen/assert";

Add Package

bunx jsr add @knudsen/assert

Import symbol

import * as mod from "@knudsen/assert";