3.1.0 • Published 1 year ago

@jclem/assert v3.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@jclem/assert

Provides a set of TypeScript-friendly functions for asserting values and types of values.

Installation

bun add @jclem/assert

Usage

This package works by making assertions about a value, and then returning the value. This means that it works differently from other type assertions in TypeScript such as asserts value is Type or value is Type. In order to make use of the type-constrained value, you must use the returned value.

import {
  assert,
  assertString,
  assertType,
  assertInstance,
} from "@jclem/assert";

class Foo {
  name = "foo";
}

function maybeValue<T>(value: T): T | null {
  return Math.random() > 0.5 ? value : null;
}

// Assert that a value is non-null and is defined.
const value = assert(maybeValue("Hello"));
value.length; // => 5

// Note that the returned value must be used.
const maybeString = maybeValue("Hello");
const definitelyString = assert(maybeString);
definitelyString.length; // => 5
// @ts-expect-error
maybeString.length; // => Error: Object is possibly 'null'.

// Assert that a value is a string
const str = assertString(maybeValue("Hello"));
str.length; // => 5

// Assert that a value is of a specific type
const bigint = assertType(maybeValue(1n), "bigint");
bigint.length; // => 1n

// Assert that a value is an instance of a class
const foo = assertInstance(maybeValue(new Foo()), Foo);
foo.name; // => 'foo'
3.1.0

1 year ago

3.0.8

1 year ago

3.0.7

1 year ago

3.0.4

1 year ago

3.0.3

1 year ago

3.0.6

1 year ago

3.0.5

1 year ago

1.2.0

2 years ago

3.0.2

1 year ago

3.0.1

1 year ago

3.0.0

1 year ago

1.2.2

2 years ago

1.2.1

2 years ago

2.0.0-alpha.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.1.0

2 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago