https://github.com/0x80/throws
An assert variant that throws only on null and undefined
https://github.com/0x80/throws
Last synced: about 2 months ago
JSON representation
An assert variant that throws only on null and undefined
- Host: GitHub
- URL: https://github.com/0x80/throws
- Owner: 0x80
- License: mit
- Created: 2024-09-29T13:57:00.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-09-29T15:07:15.000Z (7 months ago)
- Last Synced: 2025-02-25T21:41:33.657Z (2 months ago)
- Language: TypeScript
- Size: 21.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# throws
A Typescript assertion that throws only throws on null and undefined. Typically
when writing code we only want to throw on those conditions. Traditional
`assert` or `invariant` functions throw on all falsy values, and can easily trip
you up when you accidentally pass in a valid number that is 0.## Installation
```bash
pnpm add throws
```... or the equivalent for your package manager.
## Usage
```typescript
import { throws } from "throws";const valueOrUndefined: number | undefined = 0;
throws(valueOrUndefined, "This is a required value");
/**
* At this point Typescript will know that valueOrUndefined is not undefined and
* will allow you to continue writing safe code.
*/
```## No Unchecked Indexed Access
If you are using the recommended strict `noUncheckedIndexedAccess` setting, you
might want to check out [get-or-throw](https://github.com/0x80/get-or-throw).