2.0.6 • Published 4 years ago

ts-assertions v2.0.6

Weekly downloads
75
License
GPL-3.0-or-later
Repository
github
Last release
4 years ago

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.

2.0.6

4 years ago

2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago