@samual/assert
Advanced tools
+18
-1
@@ -10,3 +10,3 @@ import type { Falsy, NonFalsy } from "@samual/types"; | ||
| */ | ||
| export declare function assert<T>(value: T, message?: string | MessageFn<T & Falsy>): asserts value is NonFalsy<T>; | ||
| export declare function assert<T>(value: T, message?: string | MessageFn<T & Falsy>): asserts value; | ||
| /** | ||
@@ -18,2 +18,19 @@ * Assertion function that ensures value is not [nullish](https://developer.mozilla.org/en-US/docs/Glossary/Nullish). | ||
| /** | ||
| * An alias for {@linkcode assert()} but with a type signature that explicitly narrows with {@linkcode NonFalsy<>}. | ||
| * | ||
| * This is useful because in this code: | ||
| * ```ts | ||
| * function foo<T>(value: T) { | ||
| * assert(value) | ||
| * | ||
| * value | ||
| * // ^? (parameter) value: NonNullable<T> | ||
| * } | ||
| * ``` | ||
| * | ||
| * TypeScript incorrectly narrows the type of `value` with `NonNullable<>` when it should be using something like | ||
| * {@linkcode NonFalsy<>}. | ||
| */ | ||
| export declare const assertNonFalsy: <T>(value: T, message?: string | MessageFn<T & Falsy>) => asserts value is NonFalsy<T>; | ||
| /** | ||
| * Assertion function that ensures value is not [nullish](https://developer.mozilla.org/en-US/docs/Glossary/Nullish). | ||
@@ -20,0 +37,0 @@ * @throws If value is nullish. |
+4
-3
@@ -9,3 +9,4 @@ var AssertError = class extends Error {} | ||
| } | ||
| const expect = (value, message = "Expectation failed") => { | ||
| const assertNonFalsy = assert, | ||
| expect = (value, message = "Expectation failed") => { | ||
| assertNonNull(value, message) | ||
@@ -15,5 +16,5 @@ return value | ||
| expectTruthy = (value, message = "Truthy expectation failed") => { | ||
| assert(value, message) | ||
| assertNonFalsy(value, message) | ||
| return value | ||
| } | ||
| export { AssertError, assert, assertNonNull, expect, expectTruthy } | ||
| export { AssertError, assert, assertNonFalsy, assertNonNull, expect, expectTruthy } |
+2
-2
| { | ||
| "name": "@samual/assert", | ||
| "version": "0.0.1-655a111", | ||
| "description": "Assertion library. ", | ||
| "version": "0.0.1-adff9aa", | ||
| "description": "Assertion library.", | ||
| "keywords": [ | ||
@@ -6,0 +6,0 @@ "assertions", |
+1
-1
@@ -18,5 +18,5 @@ # Samual's Assertion Library | ||
| --- | ||
| This package is also [available on JSR](jsr). | ||
| This package is also [available on JSR][jsr]. | ||
| [npm]: https://www.npmjs.com/package/@samual | ||
| [jsr]: https://jsr.io/@sn |
4999
13.79%63
40%