Commit graph

15 commits

Author SHA1 Message Date
Joel Purra
bad42357de WIP output type of value which failed an assertion 2020-01-21 18:41:02 +01:00
Joel Purra
1e89fb4d9c Add assertion type guards
Uses the typescript v3.7 feature `asserts value is T` to create `assert` variants of the `is` type guards. The assertions are used to narrow types at compile time, and to throw `TypeError` at runtime for values which are not of the correct type.

```typescript
import {assert} from '@sindresorhus/is';

assert.string(foo);
```

- Each method in `is` is wrapped and mirrored in `assert`.
- Tests for `is` are duplicated for `assert`.

Notes

- The explicit typing in `interface Assert` is required for typescript to acknowledge the assertions.
- Due to the assertions requiring explicit typing, using ` property for is.assert.string()` (and so on) would require using `namespace is`, which was removed in #78. This also means that `assert` needs to be exported separately.
- Custom descriptions are used to enhance some assertion error messages. The value is not included in the error message.
- Could perhaps use Node.js' fitting `AssertionError` on the server-side, but it would require an `import`.

Fixes #91.

See

- https://github.com/sindresorhus/is/issues/91
- https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions
- https://github.com/microsoft/TypeScript/pull/32695
2020-01-21 18:02:59 +01:00
Joel Purra
c842cc260f Upgrade dependencies (#101) 2020-01-21 23:56:44 +07:00
Sindre Sorhus
c25b606c3b Improve the type assertion for is.asyncFunction 2019-11-07 16:37:36 +07:00
Sindre Sorhus
af6b03d67f Require Node.js 10 2019-11-07 15:58:29 +07:00
Kai Niedziela
f04dffa575 Make is.number(NaN) return false (#90)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
2019-06-30 15:09:07 +07:00
Sindre Sorhus
ffc6ce4586 Fix linting 2019-06-15 02:12:31 +07:00
Sindre Sorhus
04cd282265 Fix the capitalization of is.bigInt64Array and is.bigUint64Array 2019-05-13 11:04:48 +07:00
Sindre Sorhus
2dac8e96f4 Meta tweaks 2019-05-04 16:11:43 +07:00
Vlad Frangu
dd2a91dce5 Add support for BigInt (#87) 2019-05-04 16:05:23 +07:00
Adam Babcock
373605e40d Test coverage for is(value) (#86) 2019-04-28 15:15:11 +07:00
Sindre Sorhus
120f74ab63 Require Node.js 8 2019-04-01 04:02:25 +07:00
Sindre Sorhus
ea4204f0b4 Switch to XO for linting 2019-03-31 21:34:47 +07:00
Sindre Sorhus
0fff8265e6 Fix tests 2019-02-04 03:00:25 +07:00
Sindre Sorhus
9683cd7fd9 Run TS as a require hook for tests instead of precompile 2019-02-04 02:38:38 +07:00