ts-assertions
TypeScript icon, indicating that this package has built-in type declarations

2.0.6 • Public • Published

ts-assertions

A tiny library of assertions that can be used to validate data integrity and optionally produce type inferences of the returned value at the same time. Each of these functions throws if its conditions are not met. There is additionally an IAssertion interface which can be used externally as a generic type definition for all assertion functions, and an AssertionError class that can be used to identify any exception coming from this library.

If you want to assert the validity of a condition, use assert:

assert(condition: any, message?: string | null): true

assert(1 === 1, 'This will not throw.');
assert(1 === 2, 'This will throw.');

If you want to assert the validity of a value and have typescript respect the validated type of the , use assertValid:

assertValid<T>(value: any, message?: string | null, validator?: (value: any) => boolean): T

assertValid<number>(1, 'This will not throw.');
assertValid<boolean>(false, 'This will throw.');
assertValid<string[]>(
  [ 'one', 'two', 'three', ],
  'This will not throw.',
  (value) => (
    Array.isArray(value) && !value.find((item) => typeof item !== 'string')
  )
);

The type argument for assertValid cannot be inferred and must be provided for every usage. The default validator is coercing value to boolean and checking whether it is true.

/ts-assertions/

    Package Sidebar

    Install

    npm i ts-assertions

    Weekly Downloads

    27

    Version

    2.0.6

    License

    GPL-3.0-or-later

    Unpacked Size

    50.2 kB

    Total Files

    24

    Last publish

    Collaborators

    • furkle