Skip to content

A handful of useful TypeScript assertions for type narrowing. Published to JSR.

License

Notifications You must be signed in to change notification settings

kim-knudsen/assert

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

assert

JSR JSR Score

A handful of useful TypeScript assertions for type narrowing. Published to JSR.

Usage

import { assertTruthy } from '@knudsen/assert'

declare const x: number | string | undefined

assertTruthy(x) // number | string

assertTruthy(typeof x === 'string') // Narrows x to string

console.log(x.toUpperCase())
import { assertIsDefined } from '@knudsen/assert'

declare const user: { profileImage: string | undefined }

assertIsDefined(user.profileImage, 'profileImage') // Narrows profileImage to string

console.log('At this point we have a profile image defined', user.profileImage)
import { ensure } from '@knudsen/assert'

declare const user: { profileImage: string | undefined }

// Narrows profileImage to string and returns it
const image = ensure(user.profileImage, 'profileImage')

fetch(image).then(() => `🥳`)

About

A handful of useful TypeScript assertions for type narrowing. Published to JSR.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published