Compare commits

..

201 commits
v0.9.0 ... main

Author SHA1 Message Date
Sindre Sorhus
7821031c66 Fix CI 2026-05-11 22:57:22 +09:00
Sindre Sorhus
a439305554 8.1.0 2026-05-11 20:22:19 +09:00
Sindre Sorhus
2d4956e634 Add negative assertion helper
Fixes #220
2026-05-11 20:17:16 +09:00
Sindre Sorhus
48df5c429c 8.0.0 2026-04-09 22:09:01 +07:00
Sindre Sorhus
13febb6b01 Fix some type guards 2026-04-09 14:56:49 +07:00
Sindre Sorhus
cb4ee0e92c Fix isEnumCase incorrectly accepting numeric enum key names 2026-04-09 14:17:12 +07:00
Sindre Sorhus
54fc09406a Add negativeInteger, nonNegativeInteger, arrayOf, and oneOf predicates 2026-04-08 19:13:51 +07:00
Sindre Sorhus
63be5c0c19 Add finiteNumber, nonNegativeNumber, and positiveInteger predicates 2026-04-08 16:26:44 +07:00
Sindre Sorhus
ac46b5400d Fix isInRange silently returning false when range contains NaN 2026-04-08 15:09:53 +07:00
Sindre Sorhus
47415dc46a Fix isNumericString incorrectly accepting strings with surrounding whitespace 2026-04-08 05:51:14 +07:00
Sindre Sorhus
3b40955b02 Fix handling of functions and arrays in isEmptyObject and isNonEmptyObject 2026-04-08 05:51:13 +07:00
Sindre Sorhus
faf700367e Require Node.js 22 2026-04-08 04:38:52 +07:00
Sindre Sorhus
eff8e6b318 7.2.0 2025-12-27 11:35:17 +01:00
Sindre Sorhus
9bdcd9b57f Add predicate factory mode to is.any and is.all
Fixes #218
2025-12-27 11:33:25 +01:00
Sindre Sorhus
fbcc68e139 7.1.1 2025-11-01 00:20:45 +07:00
Tim Griesser
e7c84fcb79
Fix is.class for minified class expression (#217) 2025-11-01 00:18:25 +07:00
Sindre Sorhus
d22ab62991 7.1.0 2025-09-14 02:05:33 +07:00
Sindre Sorhus
1f2440ae0d Add is.optional and assert.optional
Fixes #111
2025-09-13 15:48:57 +07:00
Sindre Sorhus
c68ad76062 Fix TypeScript type narrowing issue with isUrlString
Fixes #212
2025-09-12 05:08:52 +07:00
Bjorn Stromberg
ef35cc350a
Structure fixtures so they can be easily tested for exclusivity (#215) 2025-06-06 13:58:46 +02:00
Sindre Sorhus
882a91c54f 7.0.2 2025-06-04 12:57:42 +03:00
Sindre Sorhus
e8e8124ba7 FIx observable checking
Fixes #214
2025-06-04 12:55:44 +03:00
Sindre Sorhus
47a5099325 Minor tweaks 2025-06-04 12:12:42 +03:00
Sindre Sorhus
e0976457e0 7.0.1 2024-09-06 22:54:53 +07:00
Martin Eneqvist
5565c5e3ba
Fix passing in assertion message to assertArray (#210)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
2024-09-06 22:52:56 +07:00
Sindre Sorhus
ab85d9bca9 7.0.0 2024-07-10 15:43:01 +02:00
Sindre Sorhus
0ff273fee8 Require Node.js 18 2024-07-10 15:41:12 +02:00
Martin Eneqvist
25a376875d
Fix type guard for isWhitespaceString and isEmptyStringOrWhitespace (#207) 2024-06-26 14:31:56 +02:00
Bjorn Stromberg
8cbcaee674
Remove deprecated methods and improve Class definition (#209) 2024-06-26 14:30:00 +02:00
Bjorn Stromberg
92699e1049
Replace ts-node with tsimp (#208) 2024-06-25 02:08:48 +02:00
Sindre Sorhus
47f49741ea 6.3.1 2024-05-16 11:15:23 +03:00
Sindre Sorhus
0df21e4151 Add missing type guard for is.enumCase
Fixes #205
2024-05-16 11:11:44 +03:00
Sindre Sorhus
f7148e19dc Meta tweaks 2024-04-29 01:32:06 +07:00
Sindre Sorhus
a1987f8bad 6.3.0 2024-04-23 12:59:46 +07:00
Martin Eneqvist
f2e5834421
Support custom assertion messages (#204)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
2024-04-23 12:58:28 +07:00
Sindre Sorhus
664b9077e1 6.2.0 2024-02-29 14:29:43 +07:00
Sindre Sorhus
07ea404e86 Meta tweaks 2024-02-29 14:26:01 +07:00
Martin Eneqvist
e9418fe1b9
Add .validDate() (#203) 2024-02-29 14:23:30 +07:00
patrik csak
0e687a23a8
Fix readme headings (#202) 2024-01-06 14:30:28 +07:00
Sindre Sorhus
1acbd9e202 6.1.0 2023-10-26 21:39:39 +07:00
Simon Podlipsky
0d4cf6fcc8
Improve TypeScript type for isNonEmptyString() and isNonEmptyStringAndNotWhitespace() (#200) 2023-10-26 21:37:39 +07:00
Sindre Sorhus
f10e2caf3d 6.0.1 2023-10-15 19:26:47 +07:00
Sindre Sorhus
e7e2213e91 directInstanceOf: Fix handling of undefined and null
Fixes #199
2023-10-15 16:01:29 +07:00
Bjorn Stromberg
9d6c91ee58
Add Node.js 20 to CI (#181) 2023-08-16 12:09:57 +02:00
Sindre Sorhus
877ed1cc6a 6.0.0 2023-08-15 20:51:44 +02:00
Sindre Sorhus
dadca59f6a Update dev dependencies 2023-08-15 20:50:56 +02:00
Bjorn Stromberg
68e0c95857
Update @sindresorhus/tsconfig to v4 (#194) 2023-08-10 16:07:56 +02:00
Bjorn Stromberg
bcec30d735
Rename is.domElement() to is.htmlElement() (#196) 2023-08-10 16:06:46 +02:00
Bjorn Stromberg
ee79af32b6
Update class method description (#195)
Co-authored-by: Harminder Virk <virk.officials@gmail.com>
2023-08-10 16:05:22 +02:00
Bjorn Stromberg
85c89925b6
Give better assertion messages for assert.all and assert.any (#193) 2023-08-09 13:49:06 +02:00
Bjorn Stromberg
e03c249d6c
Drop support for Node.js 14 (#192) 2023-08-07 17:18:36 +02:00
Bjorn Stromberg
5044c91273
Implement named exports (#191) 2023-08-07 02:50:03 +02:00
Bjorn Stromberg
bd5dfda993
Replace enums with string literals (#190) 2023-07-30 13:32:34 +02:00
Sindre Sorhus
44beb083a3 5.6.0 2023-07-23 14:40:00 +02:00
Tal Michel
3868f47783
Add .tupleLike() (#189)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
2023-07-23 14:35:23 +02:00
Sindre Sorhus
94dc715577 5.5.2 2023-07-17 11:42:55 +02:00
Eugene
20ad8231c2
Fix is.nonEmptyArray() type narrowing with undefined (#188) 2023-07-17 11:42:04 +02:00
Sindre Sorhus
ad0c3b1429 5.5.1 2023-07-16 16:20:29 +02:00
hyperbola
9d26c020ee
Fix type guards for assert.{truthy,falsy,nan} (#187) 2023-07-16 16:19:31 +02:00
Sindre Sorhus
278e0e9696 5.5.0 2023-07-15 00:28:14 +02:00
Eugene
1284da085f
Add is.positiveNumber and is.negativeNumber (#184) 2023-07-15 00:26:30 +02:00
Eugene
3177d11801
Fix type narrowing for nonEmptyArray (#185) 2023-07-15 00:24:56 +02:00
Sindre Sorhus
9265e9072d Add test for #183 2023-06-10 11:43:41 +03:00
Sindre Sorhus
7d468191f4 5.4.1 2023-06-04 11:04:07 +03:00
Bjorn Stromberg
d1574d358d
Revert exports change in package.json (#180) 2023-06-04 11:01:35 +03:00
Sindre Sorhus
61a437eba3 5.4.0 2023-05-30 21:16:59 +07:00
Sindre Sorhus
65404fbd8e Meta tweaks 2023-05-30 21:15:24 +07:00
Xananax
888e145c5a
Improve type guard/assertion of numericString() (#178) 2023-05-30 18:43:51 +07:00
Sindre Sorhus
e559b37b72 Update dev dependencies 2022-10-17 18:19:38 +07:00
Sindre Sorhus
f3693674f6 Add failing test for #174 2022-10-17 17:51:49 +07:00
Sindre Sorhus
911f44dc36 5.3.0 2022-07-21 16:30:26 +02:00
Sindre Sorhus
33cd815503 Meta tweaks 2022-07-21 16:28:44 +02:00
Sindre Sorhus
592d9093cd Make nonEmptyArray() more strongly-typed
Fixes #171
2022-07-21 16:28:43 +02:00
Sindre Sorhus
d3ff1fdfce 5.2.0 2022-06-21 14:07:38 +02:00
ehmicky
45cae31f4d
Improve plainObject() (#169) 2022-06-21 14:05:40 +02:00
Sindre Sorhus
dc99f7cd4a 5.1.0 2022-06-13 14:07:07 +07:00
Eric(书生)
e503a9ec49
Add .weakRef() (#165)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
2022-06-13 14:05:26 +07:00
Sindre Sorhus
06d217f70c 5.0.1 2022-06-11 18:28:13 +07:00
Sindre Sorhus
ac7c567c2b Fix typo 2022-06-11 18:27:36 +07:00
Sindre Sorhus
778c5da5b3 5.0.0 2022-06-11 18:19:55 +07:00
Sindre Sorhus
d6fc1ce0fe Require Node.js 14, TS 4.7, and move to ESM (#167) 2022-06-11 17:44:43 +07:00
Filip Skokan
c408f5a268
Fix NaN detection in .is() (#159) 2022-06-10 00:05:31 +07:00
Sindre Sorhus
65ea91297e 4.6.0 2022-02-27 14:21:30 +07:00
Eric(书生)
5b7ea154e6
Add .isBlob() (#162)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
2022-02-27 14:16:25 +07:00
Sindre Sorhus
6cbefb9af7 Meta tweaks 2022-02-25 16:19:29 +07:00
Sindre Sorhus
dc2dc9a438 4.5.0 2022-02-25 16:16:30 +07:00
Ivan Katliarchuk
23cf074a73
Add .nonEmptyStringAndNotWhitespace() (#161)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
2022-02-25 16:10:57 +07:00
Sindre Sorhus
04ccf21dba Document TypeScript naming limitation
Fixes #157
2022-02-01 11:37:28 +07:00
Sindre Sorhus
c3d12667fd 4.4.0 2022-01-25 17:29:45 +07:00
Zane Shannon
63d75d68ee
Add type guard for is.truthy and is.falsy (#151) 2022-01-25 17:28:33 +07:00
Pedro Augusto de Paula Barbosa
73daee6648
Improve .enumCase doc formatting in readme (#154) 2022-01-18 11:48:03 +07:00
Sindre Sorhus
b1efe7f5cf 4.3.0 2022-01-17 13:04:39 +07:00
Olivier Beaulieu
a5b4017d5e
Add is.enumCase and assert.enumCase (#150)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
2022-01-17 12:55:40 +07:00
Sindre Sorhus
f5cc764e22 4.2.1 2022-01-07 21:35:19 +07:00
Stuart Dotson
ad661ebcee
Fix is.iterable and is.asyncIterable TypeScript types (#149) 2022-01-07 21:32:06 +07:00
Sindre Sorhus
a82aeeaa5e Add FAQ about instanceof 2021-10-31 12:34:45 +07:00
Sindre Sorhus
13b2343dfc 4.2.0 2021-09-13 21:27:03 +07:00
Kayson Wu
a8de3d6f34
Add is.formData and is.urlSearchParams (#139) 2021-09-13 21:25:44 +07:00
Sindre Sorhus
b007935b4b 4.1.0 2021-09-10 15:30:39 +07:00
PopGoesTheWza
d2f98e472d
Add is.propertyKey (#138)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
Co-authored-by: Giora Guttsait <giora111@gmail.com>
2021-09-10 15:26:13 +07:00
Sindre Sorhus
6f2b24d822 Minor tweaks 2021-09-07 11:45:58 +07:00
Sindre Sorhus
238e8c80c7 4.0.1 2021-04-22 16:01:32 +07:00
Dave Cohen
b748ab72b6
Fix assertion message for .all and .any (#132) 2021-04-22 16:00:08 +07:00
Sindre Sorhus
5ed7e9bb40 Meta tweaks 2021-03-31 01:04:39 +07:00
Sindre Sorhus
4f8b01f2dc Rename master branch to main 2021-01-24 14:20:06 +07:00
Richie Bendall
da6bb531af
Move to GitHub Actions (#129) 2021-01-02 11:25:04 +07:00
Sindre Sorhus
d528545e02 4.0.0 2020-10-13 00:08:06 +02:00
Rocktim Saikia
94749dbb2e
Improve is.plainObject TypeScript type (#126) 2020-10-13 00:00:52 +02:00
Sindre Sorhus
bf6bba7af8 3.1.2 2020-08-22 00:19:30 +02:00
Arnovsky
d8ced89efe
Fix using is.array in is.all (#125) 2020-08-22 00:18:34 +02:00
Sindre Sorhus
5feadcb0b8 3.1.1 2020-08-16 12:30:54 +02:00
iyegoroff
4c29fb35cb
Improve import for VS Code (#122) 2020-08-16 12:29:46 +02:00
Sindre Sorhus
09d31733d3 3.1.0 2020-07-25 09:14:15 +02:00
Arnovsky
3f93bf200d
Add is.array overload that supports asserting array items (#119)
Co-authored-by: Pedro Augusto de Paula Barbosa <papb1996@gmail.com>
Co-authored-by: Giora Guttsait <giora111@gmail.com>
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
2020-07-25 09:11:59 +02:00
Richie Bendall
e31db97eab
Extract necessary functions from type-fest (#120) 2020-07-20 07:48:08 +08:00
Sindre Sorhus
4b35ad5bec 3.0.0 2020-06-28 04:22:15 +08:00
Bjorn Stromberg
47fa419e4f
Update devDependency @types/node from v13.7.4 to v14.0.13 (#116) 2020-06-28 04:20:49 +08:00
Bjorn Stromberg
71ca1e5573
Make the is() function type-safe (#117) 2020-06-28 04:20:36 +08:00
Bjorn Stromberg
a96abee1a3
Use shields.io for number of downloads per week (#118) 2020-06-27 19:13:23 +08:00
Bjorn Stromberg
1ffe2fb6a7
Use string literals instead of enums (#113) 2020-06-20 15:28:47 +08:00
Bjorn Stromberg
9d404cad2e
Test on Node.js 14 (#115) 2020-06-15 15:43:26 +08:00
Sindre Sorhus
fae0096eba 2.1.1 2020-04-19 18:18:00 +08:00
Arfat Salman
402fbb5a7e
Fix #109: is.numericString behaves correctly on whitespaces (#110) 2020-04-17 17:55:02 -04:00
Sindre Sorhus
05cdaccf92 Update TypeScript 2020-02-22 02:17:02 +07:00
Sindre Sorhus
1ff389cabb 2.1.0 2020-02-17 14:18:18 +07:00
Dave Cohen
d1929ad47c
Add support for multiple predicates to is.any (#104) 2020-02-16 20:06:19 -05:00
Sindre Sorhus
f97029fd73 Minor readme tweaks 2020-01-30 00:51:52 +07:00
Sindre Sorhus
863e26ad6a 2.0.0 2020-01-30 00:47:01 +07:00
Sindre Sorhus
5d0ccec21c Meta tweaks 2020-01-30 00:45:06 +07:00
Forresst
446a7a081e
Add .asyncGenerator and .asyncGeneratorFunction detection (#100) 2020-01-30 00:35:58 +07:00
Joel Purra
d28c86ee27
Propagate generic types where possible (#103) 2020-01-29 23:22:35 +07:00
Sindre Sorhus
11003b925e Meta tweaks 2020-01-22 19:17:43 +07:00
Joel Purra
0c3f110386 Add assertion type guards (#97) 2020-01-22 18:08:35 +07:00
Joel Purra
c842cc260f Upgrade dependencies (#101) 2020-01-21 23:56:44 +07:00
Sindre Sorhus
aeb3f74d65 Make the NodeStream type more accurate 2019-11-13 16:06:18 +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
Blaine Bublitz
0cc82c583e Fix a readme link (#96) 2019-10-31 11:20:49 +07:00
Sindre Sorhus
8ff75abfff Tidelift tasks 2019-10-30 20:01:23 +07:00
Sindre Sorhus
e7277de849 1.2.0 2019-10-04 11:39:58 +07:00
Sindre Sorhus
3f2caa4835 Revert "Bundle the required DOM types (#93)"
This reverts commit 7c16f20d16.
2019-10-04 11:38:10 +07:00
Sindre Sorhus
4a63743feb 1.1.0 2019-10-03 15:00:03 +07:00
Sindre Sorhus
7c16f20d16
Bundle the required DOM types (#93)
Closes #92
2019-10-02 23:06:53 +07:00
Sindre Sorhus
0f9b8479e9 Tidelift tasks 2019-09-10 12:46:18 +07:00
Sindre Sorhus
0cbd9df6ce Minor refactoring 2019-07-01 23:19:25 +07:00
Sindre Sorhus
e358e44dd5 1.0.0 2019-06-30 15:19:21 +07:00
Sindre Sorhus
4f4820ef2f Meta tweaks 2019-06-30 15:18:24 +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
7bf407fbf1
Create funding.yml 2019-05-28 12:56:17 +07:00
Sindre Sorhus
878d111ae7 0.17.1 2019-05-21 16:48:34 +07:00
Sindre Sorhus
6106086a35 Revert "Drop duplicate export (#84)"
This reverts commit 28913cae88.

Closes #89
Fixes #88
2019-05-21 16:45:07 +07:00
Sindre Sorhus
b064473589 0.17.0 2019-05-13 11:07:28 +07:00
Sindre Sorhus
04cd282265 Fix the capitalization of is.bigInt64Array and is.bigUint64Array 2019-05-13 11:04:48 +07:00
Sindre Sorhus
9bc8307770 0.16.0 2019-05-04 16:13:44 +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
Federico Brigante
28913cae88 Drop duplicate export (#84) 2019-03-14 16:32:19 +07:00
Sindre Sorhus
79144f9542 Readme tweaks 2019-02-12 14:40:44 +07:00
Sindre Sorhus
c66885b781 Add sideEffects key to package.json 2019-02-04 12:56:31 +07:00
Sindre Sorhus
0fff8265e6 Fix tests 2019-02-04 03:00:25 +07:00
Sindre Sorhus
3ec41686f7 0.15.0 2019-02-04 02:55:36 +07:00
Sindre Sorhus
2f5e03bed2 Minor tweaks 2019-02-04 02:54:30 +07:00
Sindre Sorhus
9683cd7fd9 Run TS as a require hook for tests instead of precompile 2019-02-04 02:38:38 +07:00
Sindre Sorhus
3c847be5a0 Improve the TypeScript types (#80) 2019-02-04 02:38:38 +07:00
Scottie Enriquez
2502442404 Rename .odd() to .oddInteger() and .even() to .evenInteger() for clarity (#77) 2019-02-04 02:38:38 +07:00
Sindre Sorhus
641d856b36
Stop using TypeScript namespace (#78) 2019-02-02 01:14:08 +07:00
Sindre Sorhus
ab586df0f9 Fix lint issue 2019-01-12 12:14:45 +07:00
Sindre Sorhus
0e1cce5d45
Fix readme typo 2019-01-11 16:34:29 +07:00
Itai Steinherz
4d0120adb7 Fix is.urlString documentation (#76) 2018-12-13 22:38:35 +01:00
Sindre Sorhus
6cb1d1e910 0.14.0 2018-12-13 16:58:24 +01:00
Sindre Sorhus
42fd8d3574 Use the URL global when available 2018-12-13 16:57:06 +01:00
Sindre Sorhus
844b43c9df
Improve the is.observable check (#74)
Fixes #72
2018-12-13 16:52:55 +01:00
Itai Steinherz
566f363632 Add is.urlString (#73) 2018-12-13 16:52:21 +01:00
Sindre Sorhus
2ee148f5a1 Meta tweaks 2018-12-13 00:58:02 +01:00
Sindre Sorhus
9df6f4ebe9 Bump TypeScript version 2018-11-30 15:14:41 +07:00
jokebookservice1
b8a9fb457c Minor documentational correction (#69) 2018-11-02 23:04:44 +07:00
Sindre Sorhus
2232eeea79 0.13.0 2018-11-02 19:08:53 +07:00
Sindre Sorhus
69bbf2d4be Readme improvements 2018-11-02 19:07:38 +07:00
Lukas Tetzlaff
d4869045c2 Refactor any to unknown where possible (#68)
Resolves #62
2018-11-02 17:43:51 +07:00
Itai Steinherz
7317226c80 Add is.numericString() (#67) 2018-11-01 18:21:49 +07:00
Sindre Sorhus
9ac56f1be7 Minor code style tweaks in the tests 2018-10-30 23:19:19 +07:00
Sindre Sorhus
c983ffa4cd 0.12.0 2018-09-28 13:35:34 +07:00
Sindre Sorhus
45c976071a Don't import util
We don't really need its full power. Not worth the bloat.
2018-09-28 13:32:55 +07:00
Arfat Salman
6e07df5896 Add emptiness methods (#61)
Fix #53
2018-09-28 13:24:35 +07:00
Sindre Sorhus
65c94f1a02 Include TS type lib references directly in the file
Fixes #51
2018-09-28 13:08:17 +07:00
Sam Verschueren
442f7b709f Use is-buffer ponyfill for better browser support (#66) 2018-09-27 02:02:41 +07:00
Alex Russell
ad6f372c47 Fix parameter naming of isAbsoluteMod2 function. (#64) 2018-09-18 19:22:55 +03:00
Sindre Sorhus
36b46f1889 Update dev dependencies 2018-07-31 02:17:39 +07:00
Sindre Sorhus
b2bb3e7d37 0.11.0 2018-07-10 16:56:38 +07:00
Artur Androsovych
3bdaf21475 Add is.urlInstance (#60) 2018-07-10 16:04:20 +07:00
Artur Androsovych
c8736c2972 Add is.asyncIterable (#59) 2018-07-10 00:35:16 +07:00
Sindre Sorhus
c84c2cbeca Don't publish the compiled test directory 2018-07-05 23:37:24 +07:00
Sindre Sorhus
d11b7eaaa1 0.10.0 2018-07-05 23:33:34 +07:00
Sindre Sorhus
ff268de3d0
Add related module to the readme 2018-06-30 02:07:02 +07:00
Sindre Sorhus
fe754b5528 Update dev dependencies 2018-06-01 21:28:26 +07:00
Sindre Sorhus
0988832ef7 Meta tweaks 2018-05-25 10:40:55 +07:00
Sindre Sorhus
db25d554dc TS config tweaks 2018-05-19 15:42:12 +07:00
19 changed files with 6329 additions and 1353 deletions

3
.gitattributes vendored
View file

@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf

3
.github/security.md vendored Normal file
View file

@ -0,0 +1,3 @@
# Security Policy
To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.

21
.github/workflows/main.yml vendored Normal file
View file

@ -0,0 +1,21 @@
name: CI
on:
- push
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 24
- 22
steps:
- uses: actions/checkout@v6
- uses: actions/setup-node@v6
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test

3
.gitignore vendored
View file

@ -1,3 +1,4 @@
node_modules
yarn.lock
dist
/distribution
.tsimp

View file

@ -1,4 +0,0 @@
language: node_js
node_js:
- '8'
- '6'

9
AGENTS.md Normal file
View file

@ -0,0 +1,9 @@
# Notes
## Branded types for type guards
TypeScript type guards narrow in both branches. If `is.integer(n)` returns `value is number` and the input is `number`, the false branch computes `Exclude<number, number>` = `never`. This makes common patterns like `if (!is.integer(n)) throw; use(n)` fail because `n` becomes `never` after the guard.
To avoid this, type guard predicates use branded types (e.g., `number & {readonly __brand: 'Integer'}`, `string & {readonly __brand: 'UrlString'}`). A branded subtype ensures the false branch stays the original type (e.g., `Exclude<number, Integer>` = `number`).
Assert functions (`asserts value is T`) don't need branded types since they throw on failure and have no false branch. They use plain types like `asserts value is number`.

1
CLAUDE.md Symbolic link
View file

@ -0,0 +1 @@
AGENTS.md

View file

@ -1,6 +1,6 @@
MIT License
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

View file

@ -1,26 +1,31 @@
{
"name": "@sindresorhus/is",
"version": "0.9.0",
"description": "Type check values: `is.string('🦄') //=> true`",
"version": "8.1.0",
"description": "Type check values",
"license": "MIT",
"repository": "sindresorhus/is",
"funding": "https://github.com/sindresorhus/is?sponsor=1",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"main": "dist/index.js",
"type": "module",
"exports": {
"types": "./distribution/index.d.ts",
"default": "./distribution/index.js"
},
"sideEffects": false,
"engines": {
"node": ">=6"
"node": ">=22"
},
"scripts": {
"lint": "tslint --format stylish --project .",
"build": "tsc",
"test": "npm run lint && npm run build && ava dist/tests",
"prepublish": "npm run build && del dist/tests"
"build": "del distribution && tsc",
"test": "tsc --noEmit && tsc --project test/tsconfig.json --noEmit && xo && node --experimental-transform-types --test test/test.ts",
"prepare": "npm run build"
},
"files": [
"dist"
"distribution"
],
"keywords": [
"type",
@ -41,25 +46,31 @@
"kind",
"primitive",
"verify",
"compare"
"compare",
"typescript",
"typeguards",
"types"
],
"dependencies": {
"symbol-observable": "^1.2.0"
"xo": {
"rules": {
"@typescript-eslint/no-unsafe-enum-comparison": "off",
"@typescript-eslint/no-confusing-void-expression": "off",
"@typescript-eslint/no-unsafe-type-assertion": "off",
"@stylistic/operator-linebreak": "off"
}
},
"devDependencies": {
"@types/jsdom": "^11.0.4",
"@types/node": "^9.6.0",
"@types/tempy": "^0.1.0",
"@types/zen-observable": "^0.5.3",
"ava": "*",
"del-cli": "^1.1.0",
"jsdom": "^11.6.2",
"rxjs": "^6.0.0",
"tempy": "^0.2.1",
"tslint": "^5.9.1",
"tslint-xo": "^0.7.0",
"typescript": "^2.8.1",
"zen-observable": "^0.8.8"
},
"types": "dist/index.d.ts"
"@sindresorhus/tsconfig": "^8.1.0",
"@types/jsdom": "^28.0.1",
"@types/node": "^25.5.2",
"@types/zen-observable": "^0.8.7",
"del-cli": "^7.0.0",
"expect-type": "^1.3.0",
"jsdom": "^29.0.2",
"rxjs": "^7.8.2",
"tempy": "^3.2.0",
"typescript": "6.0.2",
"xo": "^2.0.2",
"zen-observable": "^0.10.0"
}
}

1049
readme.md

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -1,781 +0,0 @@
import * as fs from 'fs';
import * as net from 'net';
import * as Stream from 'stream';
import * as util from 'util';
import * as tempy from 'tempy';
import test, {TestContext, Context} from 'ava';
import {JSDOM} from 'jsdom';
import {Subject, Observable} from 'rxjs';
import ZenObservable from 'zen-observable';
import m from '..';
const isNode8orHigher = Number(process.versions.node.split('.')[0]) >= 8;
class PromiseSubclassFixture<T> extends Promise<T> {}
class ErrorSubclassFixture extends Error {}
const {window} = new JSDOM();
const {document} = window;
const createDomElement = (el: string) => document.createElement(el);
interface Test {
is(value: any): boolean;
fixtures: any[];
}
const types = new Map<string, Test>([
['undefined', {
is: m.undefined,
fixtures: [
undefined
]
}],
['null', {
is: m.null_,
fixtures: [
null
]
}],
['string', {
is: m.string,
fixtures: [
'🦄',
'hello world',
''
]
}],
['number', {
is: m.number,
fixtures: [
6,
1.4,
0,
-0,
Infinity,
-Infinity
]
}],
['boolean', {
is: m.boolean,
fixtures: [
true, false
]
}],
['symbol', {
is: m.symbol,
fixtures: [
Symbol('🦄')
]
}],
['array', {
is: m.array,
fixtures: [
[1, 2],
new Array(2) // tslint:disable-line:prefer-array-literal
]
}],
['function', {
is: m.function_,
fixtures: [
// tslint:disable:no-unused no-empty no-unused-variable only-arrow-functions no-function-expression
function foo() {},
function () {},
() => {},
async function () {},
function * (): any {}
// tslint:enable:no-unused no-empty no-unused-variable only-arrow-functions no-function-expression
]
}],
['buffer', {
is: m.buffer,
fixtures: [
Buffer.from('🦄')
]
}],
['object', {
is: m.object,
fixtures: [
{x: 1},
Object.create({x: 1})
]
}],
['regExp', {
is: m.regExp,
fixtures: [
/\w/,
new RegExp('\\w')
]
}],
['date', {
is: m.date,
fixtures: [
new Date()
]
}],
['error', {
is: m.error,
fixtures: [
new Error('🦄'),
new ErrorSubclassFixture()
]
}],
['nativePromise', {
is: m.nativePromise,
fixtures: [
Promise.resolve(),
PromiseSubclassFixture.resolve()
]
}],
['promise', {
is: m.promise,
fixtures: [
{then() {}, catch() {}} // tslint:disable-line:no-empty
]
}],
['generator', {
is: m.generator,
fixtures: [
(function * () {
yield 4;
})()
]
}],
['generatorFunction', {
is: m.generatorFunction,
fixtures: [
function * () {
yield 4;
}
]
}],
['asyncFunction', {
is: m.asyncFunction,
fixtures: [
async function () {}, // tslint:disable-line:no-empty only-arrow-functions no-function-expression
async () => {} // tslint:disable-line:no-empty
]
}],
['boundFunction', {
is: m.boundFunction,
fixtures: [
() => {}, // tslint:disable-line:no-empty
function () {}.bind(null), // tslint:disable-line:no-empty only-arrow-functions
]
}],
['map', {
is: m.map,
fixtures: [
new Map()
]
}],
['set', {
is: m.set,
fixtures: [
new Set()
]
}],
['weakSet', {
is: m.weakSet,
fixtures: [
new WeakSet()
]
}],
['weakMap', {
is: m.weakMap,
fixtures: [
new WeakMap()
]
}],
['int8Array', {
is: m.int8Array,
fixtures: [
new Int8Array(0)
]
}],
['uint8Array', {
is: m.uint8Array,
fixtures: [
new Uint8Array(0)
]
}],
['uint8ClampedArray', {
is: m.uint8ClampedArray,
fixtures: [
new Uint8ClampedArray(0)
]
}],
['int16Array', {
is: m.int16Array,
fixtures: [
new Int16Array(0)
]
}],
['uint16Array', {
is: m.uint16Array,
fixtures: [
new Uint16Array(0)
]
}],
['int32Array', {
is: m.int32Array,
fixtures: [
new Int32Array(0)
]
}],
['uint32Array', {
is: m.uint32Array,
fixtures: [
new Uint32Array(0)
]
}],
['float32Array', {
is: m.float32Array,
fixtures: [
new Float32Array(0)
]
}],
['float64Array', {
is: m.float64Array,
fixtures: [
new Float64Array(0)
]
}],
['arrayBuffer', {
is: m.arrayBuffer,
fixtures: [
new ArrayBuffer(10)
]
}],
['dataView', {
is: m.dataView,
fixtures: [
new DataView(new ArrayBuffer(10))
]
}],
['nan', {
is: m.nan,
fixtures: [
NaN,
Number.NaN
]
}],
['nullOrUndefined', {
is: m.nullOrUndefined,
fixtures: [
null,
undefined
]
}],
['plainObject', {
is: m.plainObject,
fixtures: [
{x: 1},
Object.create(null),
new Object()
]
}],
['integer', {
is: m.integer,
fixtures: [
6
]
}],
['safeInteger', {
is: m.safeInteger,
fixtures: [
Math.pow(2, 53) - 1,
-Math.pow(2, 53) + 1
]
}],
['domElement', {
is: m.domElement,
fixtures: [
'div',
'input',
'span',
'img',
'canvas',
'script'
].map(createDomElement) }
],
['non-domElements', {
is: value => !m.domElement(value),
fixtures: [
document.createTextNode('data'),
document.createProcessingInstruction('xml-stylesheet', 'href="mycss.css" type="text/css"'),
document.createComment('This is a comment'),
document,
document.implementation.createDocumentType('svg:svg', '-//W3C//DTD SVG 1.1//EN', 'https://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'),
document.createDocumentFragment()
]
}],
['nodeStream', {
is: m.nodeStream,
fixtures: [
fs.createReadStream('readme.md'),
fs.createWriteStream(tempy.file()),
new net.Socket(),
new Stream.Duplex(),
new Stream.PassThrough(),
new Stream.Readable(),
new Stream.Transform(),
new Stream.Stream(),
new Stream.Writable()
]
}],
['observable', {
is: m.observable,
fixtures: [
new Observable(),
new Subject(),
new ZenObservable(() => {}) // tslint:disable-line:no-empty
]
}],
['infinite', {
is: m.infinite,
fixtures: [
Infinity,
-Infinity
]
}]
]);
// This ensures a certain method matches only the types it's supposed to and none of the other methods' types
const testType = (t: TestContext & Context<any>, type: string, exclude?: string[]) => {
const testData = types.get(type);
if (testData === undefined) {
t.fail(`is.${type} not defined`);
return;
}
const {is} = testData;
for (const [key, {fixtures}] of types) {
// TODO: Automatically exclude value types in other tests that we have in the current one.
// Could reduce the use of `exclude`.
if (exclude && exclude.indexOf(key) !== -1) {
continue;
}
const assert = key === type ? t.true.bind(t) : t.false.bind(t);
for (const fixture of fixtures) {
assert(is(fixture), `Value: ${util.inspect(fixture)}`);
}
}
};
test('is', t => {
t.is(m(null), 'null');
t.is(m(undefined), 'undefined');
// TODO: Expand this to all the supported types. Maybe reuse `testType()` somehow.
});
test('is.undefined', t => {
testType(t, 'undefined', ['nullOrUndefined']);
});
test('is.null', t => {
testType(t, 'null', ['nullOrUndefined']);
});
test('is.string', t => {
testType(t, 'string');
});
test('is.number', t => {
testType(t, 'number', ['nan', 'integer', 'safeInteger', 'infinite']);
});
test('is.boolean', t => {
testType(t, 'boolean');
});
test('is.symbol', t => {
testType(t, 'symbol');
});
test('is.array', t => {
testType(t, 'array');
});
test('is.function', t => {
testType(t, 'function', ['generatorFunction', 'asyncFunction', 'boundFunction']);
});
test('is.boundFunction', t => {
t.false(m.boundFunction(function () {})); // tslint:disable-line:no-empty only-arrow-functions
});
test('is.buffer', t => {
testType(t, 'buffer');
});
test('is.object', t => {
const testData = types.get('object');
if (testData === undefined) {
t.fail('is.object not defined');
return;
}
for (const el of testData.fixtures) {
t.true(m.object(el));
}
});
test('is.regExp', t => {
testType(t, 'regExp');
});
test('is.date', t => {
testType(t, 'date');
});
test('is.error', t => {
testType(t, 'error');
});
if (isNode8orHigher) {
test('is.nativePromise', t => {
testType(t, 'nativePromise');
});
test('is.promise', t => {
testType(t, 'promise', ['nativePromise']);
});
/*test('is.asyncFunction', t => {
testType(t, 'asyncFunction', ['function']);
});*/
}
test('is.generator', t => {
testType(t, 'generator');
});
test('is.generatorFunction', t => {
testType(t, 'generatorFunction', ['function']);
});
test('is.map', t => {
testType(t, 'map');
});
test('is.set', t => {
testType(t, 'set');
});
test('is.weakMap', t => {
testType(t, 'weakMap');
});
test('is.weakSet', t => {
testType(t, 'weakSet');
});
test('is.int8Array', t => {
testType(t, 'int8Array');
});
test('is.uint8Array', t => {
testType(t, 'uint8Array', ['buffer']);
});
test('is.uint8ClampedArray', t => {
testType(t, 'uint8ClampedArray');
});
test('is.int16Array', t => {
testType(t, 'int16Array');
});
test('is.uint16Array', t => {
testType(t, 'uint16Array');
});
test('is.int32Array', t => {
testType(t, 'int32Array');
});
test('is.uint32Array', t => {
testType(t, 'uint32Array');
});
test('is.float32Array', t => {
testType(t, 'float32Array');
});
test('is.float64Array', t => {
testType(t, 'float64Array');
});
test('is.arrayBuffer', t => {
testType(t, 'arrayBuffer');
});
test('is.dataView', t => {
testType(t, 'dataView');
});
test('is.directInstanceOf', t => {
const error = new Error();
const errorSubclass = new ErrorSubclassFixture();
t.true(m.directInstanceOf(error, Error));
t.true(m.directInstanceOf(errorSubclass, ErrorSubclassFixture));
t.false(m.directInstanceOf(error, ErrorSubclassFixture));
t.false(m.directInstanceOf(errorSubclass, Error));
});
test('is.truthy', t => {
t.true(m.truthy('unicorn'));
t.true(m.truthy('🦄'));
t.true(m.truthy(new Set()));
t.true(m.truthy(Symbol('🦄')));
t.true(m.truthy(true));
});
test('is.falsy', t => {
t.true(m.falsy(false));
t.true(m.falsy(0));
t.true(m.falsy(''));
t.true(m.falsy(null));
t.true(m.falsy(undefined));
t.true(m.falsy(NaN));
});
test('is.nan', t => {
testType(t, 'nan');
});
test('is.nullOrUndefined', t => {
testType(t, 'nullOrUndefined', ['undefined', 'null']);
});
test('is.primitive', t => {
const primitives = [
undefined,
null,
'🦄',
6,
Infinity,
-Infinity,
true,
false,
Symbol('🦄')
];
for (const el of primitives) {
t.true(m.primitive(el));
}
});
test('is.integer', t => {
testType(t, 'integer', ['number', 'safeInteger']);
t.false(m.integer(1.4));
});
test('is.safeInteger', t => {
testType(t, 'safeInteger', ['number', 'integer']);
t.false(m.safeInteger(Math.pow(2, 53)));
t.false(m.safeInteger(-Math.pow(2, 53)));
});
test('is.plainObject', t => {
testType(t, 'plainObject', ['object', 'promise']);
});
test('is.iterable', t => {
t.true(m.iterable(''));
t.true(m.iterable([]));
t.true(m.iterable(new Map()));
t.false(m.iterable(null));
t.false(m.iterable(undefined));
t.false(m.iterable(0));
t.false(m.iterable(NaN));
t.false(m.iterable(Infinity));
t.false(m.iterable({}));
});
test('is.class', t => {
class Foo {} // tslint:disable-line
const classDeclarations = [
Foo,
class Bar extends Foo {} // tslint:disable-line
];
for (const x of classDeclarations) {
t.true(m.class_(x));
}
});
test('is.typedArray', t => {
// Typescript currently does not support empty constructors for these
// See https://github.com/Microsoft/TypeScript/issues/19680
const typedArrays = [
new Int8Array(0),
new Uint8Array(0),
new Uint8ClampedArray(0),
new Uint16Array(0),
new Int32Array(0),
new Uint32Array(0),
new Float32Array(0),
new Float64Array(0)
];
for (const el of typedArrays) {
t.true(m.typedArray(el));
}
t.false(m.typedArray(new ArrayBuffer(1)));
t.false(m.typedArray([]));
t.false(m.typedArray({}));
});
test('is.arrayLike', t => {
(function () { // tslint:disable-line:only-arrow-functions
t.true(m.arrayLike(arguments));
})();
t.true(m.arrayLike([]));
t.true(m.arrayLike('unicorn'));
t.false(m.arrayLike({}));
t.false(m.arrayLike(() => {})); // tslint:disable-line:no-empty
t.false(m.arrayLike(new Map()));
});
test('is.inRange', t => {
const x = 3;
t.true(m.inRange(x, [0, 5]));
t.true(m.inRange(x, [5, 0]));
t.true(m.inRange(x, [-5, 5]));
t.true(m.inRange(x, [5, -5]));
t.false(m.inRange(x, [4, 8]));
t.true(m.inRange(-7, [-5, -10]));
t.true(m.inRange(-5, [-5, -10]));
t.true(m.inRange(-10, [-5, -10]));
t.true(m.inRange(x, 10));
t.true(m.inRange(0, 0));
t.true(m.inRange(-2, -3));
t.false(m.inRange(x, 2));
t.false(m.inRange(-3, -2));
t.throws(() => {
m.inRange(0, []);
});
t.throws(() => {
m.inRange(0, [5]);
});
t.throws(() => {
m.inRange(0, [1, 2, 3]);
});
});
test('is.domElement', t => {
testType(t, 'domElement');
t.false(m.domElement({nodeType: 1, nodeName: 'div'}));
});
test('is.nodeStream', t => {
testType(t, 'nodeStream');
});
test('is.infinite', t => {
testType(t, 'infinite', ['number']);
});
test('is.even', t => {
for (const el of [-6, 2, 4]) {
t.true(m.even(el));
}
for (const el of [-3, 1, 5]) {
t.false(m.even(el));
}
});
test('is.odd', t => {
for (const el of [-5, 7, 13]) {
t.true(m.odd(el));
}
for (const el of [-8, 8, 10]) {
t.false(m.odd(el));
}
});
test('is.empty', t => {
t.true(m.empty(null));
t.true(m.empty(undefined));
t.true(m.empty(false));
t.false(m.empty(true));
t.true(m.empty(''));
t.false(m.empty('🦄'));
t.true(m.empty([]));
t.false(m.empty(['🦄']));
t.true(m.empty({}));
t.false(m.empty({unicorn: '🦄'}));
const tempMap = new Map();
t.true(m.empty(tempMap));
tempMap.set('unicorn', '🦄');
t.false(m.empty(tempMap));
const tempSet = new Set();
t.true(m.empty(tempSet));
tempSet.add(1);
t.false(m.empty(tempSet));
});
test('is.emptyOrWhitespace', t => {
t.true(m.emptyOrWhitespace(''));
t.true(m.emptyOrWhitespace(' '));
t.false(m.emptyOrWhitespace('🦄'));
t.false(m.emptyOrWhitespace('unicorn'));
});
test('is.any', t => {
t.true(m.any(m.string, {}, true, '🦄'));
t.true(m.any(m.object, false, {}, 'unicorns'));
t.false(m.any(m.boolean, '🦄', [], 3));
t.false(m.any(m.integer, true, 'lol', {}));
t.throws(() => {
m.any(null, true);
});
t.throws(() => {
m.any(m.string);
});
});
test('is.all', t => {
t.true(m.all(m.object, {}, new Set(), new Map()));
t.true(m.all(m.boolean, true, false));
t.false(m.all(m.string, '🦄', []));
t.false(m.all(m.set, new Map(), {}));
t.throws(() => {
m.all(null, true);
});
t.throws(() => {
m.all(m.string);
});
});

199
source/types.ts Normal file
View file

@ -0,0 +1,199 @@
// Extracted from https://github.com/sindresorhus/type-fest/blob/78019f42ea888b0cdceb41a4a78163868de57555/index.d.ts
/**
Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
*/
export type Primitive =
// eslint-disable-next-line @typescript-eslint/no-restricted-types
| null
| undefined
| string
| number
| boolean
| symbol
| bigint;
/**
Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
*/
type Constructor<T, Arguments extends unknown[] = any[]> = new(...arguments_: Arguments) => T;
/**
Matches a [`class`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
*/
export type Class<T, Arguments extends unknown[] = any[]> = Constructor<T, Arguments> & {prototype: T};
/**
Matches any [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray), like `Uint8Array` or `Float64Array`.
*/
export type TypedArray =
| Int8Array
| Uint8Array
| Uint8ClampedArray
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Float32Array
| Float64Array
| BigInt64Array
| BigUint64Array;
declare global {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions -- This must be an `interface` so it can be merged.
interface SymbolConstructor {
readonly observable: symbol;
}
}
/**
Matches a value that is like an [Observable](https://github.com/tc39/proposal-observable).
*/
export type ObservableLike = {
subscribe(observer: (value: unknown) => void): void;
[Symbol.observable](): ObservableLike;
};
// eslint-disable-next-line @typescript-eslint/no-restricted-types
export type Falsy = false | 0 | 0n | '' | null | undefined;
// eslint-disable-next-line @typescript-eslint/no-restricted-types
export type WeakRef<T extends object> = {
readonly [Symbol.toStringTag]: 'WeakRef';
deref(): T | undefined;
};
export type ArrayLike<T> = {
readonly [index: number]: T;
readonly length: number;
};
export type NodeStream = {
pipe<T extends NodeJS.WritableStream>(destination: T, options?: {end?: boolean}): T;
} & NodeJS.EventEmitter;
export type Predicate = (value: unknown) => boolean;
export type NonEmptyString = string & {0: string};
export type Whitespace = ' ';
type Brand<Key extends string> = Readonly<Record<Key, true>>;
/**
A string that represents a valid URL.
This is a branded type to prevent incorrect TypeScript type narrowing.
*/
export type UrlString = string & {readonly __brand: 'UrlString'};
// Keep numeric guards branded and simple. This intentionally favors correct false-branch narrowing for `number` inputs over perfect success-branch narrowing for numeric literal unions.
/**
The IEEE 754 "Not-a-Number" value, typed as a subtype of `number`.
Branded to prevent false-branch narrowing to `never` when the input is `number`.
*/
export type NaN = number & Brand<'__nanBrand'>;
/**
A finite number (excludes `NaN`, `Infinity`, and `-Infinity`).
Branded to prevent false-branch narrowing to `never` when the input is `number`.
*/
export type FiniteNumber = number & Brand<'__finiteNumberBrand'>;
/**
A number greater than or equal to zero.
Branded to prevent false-branch narrowing to `never` when the input is `number`.
*/
export type NonNegativeNumber = number & Brand<'__nonNegativeNumberBrand'>;
/**
An integer value (no fractional part).
Branded to prevent false-branch narrowing to `never` when the input is `number`.
*/
export type Integer = FiniteNumber & Brand<'__integerBrand'>;
/**
A number greater than zero.
Branded to prevent false-branch narrowing to `never` when the input is `number`.
*/
export type PositiveNumber = NonNegativeNumber & Brand<'__positiveNumberBrand'>;
/**
A number less than zero.
Branded to prevent false-branch narrowing to `never` when the input is `number`.
*/
export type NegativeNumber = number & Brand<'__negativeNumberBrand'>;
/**
An integer less than zero.
Branded to prevent false-branch narrowing to `never` when the input is `number`.
*/
export type NegativeInteger = Integer & NegativeNumber & Brand<'__negativeIntegerBrand'>;
/**
An integer greater than or equal to zero.
Branded to prevent false-branch narrowing to `never` when the input is `number`.
*/
export type NonNegativeInteger = Integer & NonNegativeNumber & Brand<'__nonNegativeIntegerBrand'>;
/**
An integer greater than zero.
Branded to prevent false-branch narrowing to `never` when the input is `number`.
*/
export type PositiveInteger = NonNegativeInteger & PositiveNumber & Brand<'__positiveIntegerBrand'>;
// Note: type-fest uses the `1e999` overflow trick to represent these types (since TypeScript has
// no built-in Infinity type), but we use branded types here for consistency and to avoid
// relying on numeric overflow behavior.
/**
A positive infinite number (`Infinity`).
Branded to prevent false-branch narrowing to `never` when the input is `number`.
*/
export type PositiveInfinity = PositiveNumber & Brand<'__positiveInfinityBrand'>;
/**
A negative infinite number (`-Infinity`).
Branded to prevent false-branch narrowing to `never` when the input is `number`.
*/
export type NegativeInfinity = NegativeNumber & Brand<'__negativeInfinityBrand'>;
/**
A safe integer (within the range of `Number.MIN_SAFE_INTEGER` to `Number.MAX_SAFE_INTEGER`).
Branded to prevent false-branch narrowing to `never` when the input is `number`.
*/
export type SafeInteger = Integer & Brand<'__safeIntegerBrand'>;
/**
An even integer.
Branded to prevent false-branch narrowing to `never` when the input is `number`.
*/
export type EvenInteger = Integer & Brand<'__evenIntegerBrand'>;
/**
An odd integer.
Branded to prevent false-branch narrowing to `never` when the input is `number`.
*/
export type OddInteger = Integer & Brand<'__oddIntegerBrand'>;
/**
A non-negative safe integer, suitable as an array or string length.
Branded to prevent false-branch narrowing to `never` when the input is `number`.
*/
export type ValidLength = SafeInteger & NonNegativeInteger & Brand<'__validLengthBrand'>;

3
source/utilities.ts Normal file
View file

@ -0,0 +1,3 @@
export function keysOf<T extends Record<PropertyKey, unknown>>(value: T): Array<keyof T> {
return Object.keys(value) as Array<keyof T>; // eslint-disable-line @typescript-eslint/no-unnecessary-type-assertion
}

2822
test/test.ts Normal file

File diff suppressed because it is too large Load diff

12
test/tsconfig.json Normal file
View file

@ -0,0 +1,12 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"rootDir": "..",
"noUnusedLocals": false,
"noUnusedParameters": false
},
"include": [
"../source",
"type-tests.ts"
]
}

405
test/type-tests.ts Normal file
View file

@ -0,0 +1,405 @@
import {expectTypeOf} from 'expect-type';
import is, {
assert as isAssert,
assertNotNullOrUndefined,
assertNotPrimitive,
assertNotString,
assertNotUndefined,
type EvenInteger,
type FiniteNumber,
type Integer,
type NaN as NaNType,
type NegativeInfinity,
type NegativeInteger,
type NegativeNumber,
type NonNegativeInteger,
type NonNegativeNumber,
type OddInteger,
type PositiveInfinity,
type PositiveInteger,
type PositiveNumber,
type Primitive,
type SafeInteger,
type ValidLength,
} from '../source/index.ts';
// eslint-disable-next-line @typescript-eslint/no-restricted-types
type UnknownNotPrimitive<Forbidden extends Primitive> = Exclude<Primitive, Forbidden> | object;
// For each predicate, verify two things:
// 1. True branch narrows to the branded type.
// 2. False branch on a `number` input stays `number` (not `never`).
// Without the branded types, `Exclude<number, number>` = `never` would break
// the common validation-guard pattern: if (!is.X(n)) throw; use(n).
const nanCheck = (value: number) => {
if (is.nan(value)) {
const _: NaNType = value;
} else {
const _: number = value;
}
};
const finiteNumberCheck = (value: number) => {
if (is.finiteNumber(value)) {
const _: FiniteNumber = value;
} else {
const _: number = value;
}
};
const nonNegativeNumberCheck = (value: number) => {
if (is.nonNegativeNumber(value)) {
const _: NonNegativeNumber = value;
} else {
const _: number = value;
}
};
const positiveIntegerCheck = (value: number) => {
if (is.positiveInteger(value)) {
const _: PositiveInteger = value;
const __: Integer = value;
const ___: NonNegativeInteger = value;
} else {
const _: number = value;
}
};
const negativeIntegerCheck = (value: number) => {
if (is.negativeInteger(value)) {
const _: NegativeInteger = value;
const __: Integer = value;
} else {
const _: number = value;
}
};
const nonNegativeIntegerCheck = (value: number) => {
if (is.nonNegativeInteger(value)) {
const _: NonNegativeInteger = value;
const __: Integer = value;
} else {
const _: number = value;
}
};
const infiniteCheck = (value: number) => {
if (is.infinite(value)) {
const _: PositiveInfinity | NegativeInfinity = value;
const __: PositiveNumber | NegativeNumber = value;
} else {
const _: number = value;
}
};
const integerCheck = (value: number) => {
if (is.integer(value)) {
const _: Integer = value;
const __: FiniteNumber = value;
} else {
const _: number = value;
}
};
const safeIntegerCheck = (value: number) => {
if (is.safeInteger(value)) {
const _: SafeInteger = value;
const __: Integer = value;
} else {
const _: number = value;
}
};
const evenIntegerCheck = (value: number) => {
if (is.evenInteger(value)) {
const _: EvenInteger = value;
const __: Integer = value;
} else {
const _: number = value;
}
};
const oddIntegerCheck = (value: number) => {
if (is.oddInteger(value)) {
const _: OddInteger = value;
const __: Integer = value;
} else {
const _: number = value;
}
};
const positiveNumberCheck = (value: number) => {
if (is.positiveNumber(value)) {
const _: PositiveNumber = value;
const __: NonNegativeNumber = value;
} else {
const _: number = value;
}
};
const negativeNumberCheck = (value: number) => {
if (is.negativeNumber(value)) {
const _: NegativeNumber = value;
} else {
const _: number = value;
}
};
const validLengthCheck = (value: number) => {
if (is.validLength(value)) {
const _: ValidLength = value;
const __: SafeInteger = value;
const ___: NonNegativeInteger = value;
} else {
const _: number = value;
}
};
const integerUnknownCheck = (value: unknown) => {
if (is.integer(value)) {
const _: Integer = value;
const __: FiniteNumber = value;
}
};
const positiveIntegerUnknownCheck = (value: unknown) => {
if (is.positiveInteger(value)) {
const _: PositiveInteger = value;
const __: NonNegativeInteger = value;
}
};
const integerMixedUnionCheck = (value: string | number) => {
if (is.integer(value)) {
const _: number = value;
} else {
const _: string = value;
}
};
const positiveNumberMixedUnionCheck = (value: string | number) => {
if (is.positiveNumber(value)) {
const _: number = value;
} else {
const _: string = value;
}
};
const chainedNumericGuardCheck = (value: number) => {
if (is.positiveNumber(value) && is.integer(value)) {
const _: PositiveNumber = value;
const __: Integer = value;
const ___: FiniteNumber = value;
}
};
const distinctNumericBrandsStayDistinct = (
positiveInteger: PositiveInteger,
negativeInteger: NegativeInteger,
validLength: ValidLength,
) => {
// @ts-expect-error -- Distinct numeric refinements must not collapse into each other.
const _: NegativeInteger = positiveInteger;
// @ts-expect-error -- ValidLength is non-negative and must not become a signed integer refinement.
const __: NegativeInteger = validLength;
return negativeInteger;
};
const assertNotUndefinedCheck = (value: string | undefined) => {
isAssert.not.undefined(value);
expectTypeOf(value).toEqualTypeOf<string>();
};
const assertNotUndefinedUnknownCheck = (value: unknown) => {
isAssert.not.undefined(value);
expectTypeOf(value).toEqualTypeOf<UnknownNotPrimitive<undefined>>();
};
const assertNotUndefinedGenericCheck = <T>(value: T) => {
isAssert.not.undefined(value);
const _: Exclude<T, undefined> = value;
};
const nullValue = null;
type Null = typeof nullValue;
const assertNotNullUnknownCheck = (value: unknown) => {
isAssert.not.null(value);
expectTypeOf(value).toEqualTypeOf<UnknownNotPrimitive<Null>>();
};
const assertNotNullOrUndefinedCheck = (value: string | Null | undefined) => {
isAssert.not.nullOrUndefined(value);
expectTypeOf(value).toEqualTypeOf<string>();
};
const assertNotNullOrUndefinedUnknownCheck = (value: unknown) => {
isAssert.not.nullOrUndefined(value);
expectTypeOf(value).toEqualTypeOf<UnknownNotPrimitive<Null | undefined>>();
};
const assertNotStringCheck = (value: string | number) => {
isAssert.not.string(value);
expectTypeOf(value).toEqualTypeOf<number>();
};
const assertNotStringUnknownCheck = (value: unknown) => {
isAssert.not.string(value);
expectTypeOf(value).toEqualTypeOf<UnknownNotPrimitive<string>>();
};
const assertNotStringGenericCheck = <T>(value: T) => {
isAssert.not.string(value);
const _: Exclude<T, string> = value;
};
const assertNotBooleanUnknownCheck = (value: unknown) => {
isAssert.not.boolean(value);
expectTypeOf(value).toEqualTypeOf<UnknownNotPrimitive<boolean>>();
};
const assertNotSymbolUnknownCheck = (value: unknown) => {
isAssert.not.symbol(value);
expectTypeOf(value).toEqualTypeOf<UnknownNotPrimitive<symbol>>();
};
const assertNotBigintUnknownCheck = (value: unknown) => {
isAssert.not.bigint(value);
expectTypeOf(value).toEqualTypeOf<UnknownNotPrimitive<bigint>>();
};
const assertNotPrimitiveUnknownCheck = (value: unknown) => {
isAssert.not.primitive(value);
// eslint-disable-next-line @typescript-eslint/no-restricted-types
expectTypeOf(value).toEqualTypeOf<object>();
};
const assertNotPrimitiveGenericCheck = <T>(value: T) => {
isAssert.not.primitive(value);
const _: Exclude<T, Primitive> = value;
};
const assertNotNamedUndefinedExportCheck = (value: 0 | false | '' | Null | undefined | 'ok') => {
assertNotUndefined(value);
expectTypeOf(value).toEqualTypeOf<0 | false | '' | Null | 'ok'>();
};
const assertNotNamedNullOrUndefinedUnknownExportCheck = (value: unknown) => {
assertNotNullOrUndefined(value);
expectTypeOf(value).toEqualTypeOf<UnknownNotPrimitive<Null | undefined>>();
};
const assertNotNamedStringExportCheck = (value: string | number) => {
assertNotString(value);
expectTypeOf(value).toEqualTypeOf<number>();
};
const assertNotNamedStringUnknownExportCheck = (value: unknown) => {
assertNotString(value);
expectTypeOf(value).toEqualTypeOf<UnknownNotPrimitive<string>>();
};
const assertNotNamedPrimitiveUnknownExportCheck = (value: unknown) => {
assertNotPrimitive(value);
// eslint-disable-next-line @typescript-eslint/no-restricted-types
expectTypeOf(value).toEqualTypeOf<object>();
};
const assertNotCallableDoesNotExistCheck = (value: string | undefined) => {
// @ts-expect-error -- Generic negative assertions cannot safely infer complement types from arbitrary predicates.
isAssert.not(is.undefined, value);
const _: string | undefined = value;
};
const assertNotNumberDoesNotExistCheck = (value: string | number) => {
// @ts-expect-error -- `is.number` rejects `NaN`, so a narrowing negative assertion would be unsound.
isAssert.not.number(value); // eslint-disable-line @typescript-eslint/no-unsafe-call
const _: string | number = value;
};
const assertNotIntegerDoesNotExistCheck = (value: string | number) => {
// @ts-expect-error -- Numeric refinements are intentionally excluded from `assert.not`.
isAssert.not.integer(value); // eslint-disable-line @typescript-eslint/no-unsafe-call
const _: string | number = value;
};
const assertNotObjectDoesNotExistCheck = (value: Record<string, unknown> | string) => {
// @ts-expect-error -- TypeScript's `{}` type includes primitives, so `not.object` cannot safely narrow every object-like input.
isAssert.not.object(value); // eslint-disable-line @typescript-eslint/no-unsafe-call
const _: Record<string, unknown> | string = value;
};
const assertNotBlobDoesNotExistCheck = (value: Blob | File | string) => {
// @ts-expect-error -- `File` extends `Blob` in TypeScript but does not match the exact runtime `Blob` check.
isAssert.not.blob(value); // eslint-disable-line @typescript-eslint/no-unsafe-call
const _: Blob | File | string = value;
};
const assertNotMapDoesNotExistCheck = (value: Map<string, number> | string) => {
// @ts-expect-error -- Structural object types such as `Map` can be assignable in TypeScript without matching the runtime brand check.
isAssert.not.map(value); // eslint-disable-line @typescript-eslint/no-unsafe-call, unicorn/no-array-callback-reference
const _: Map<string, number> | string = value;
};
const assertNotSetDoesNotExistCheck = (value: Set<string> | string) => {
// @ts-expect-error -- Structural object types such as `Set` can be assignable in TypeScript without matching the runtime brand check.
isAssert.not.set(value); // eslint-disable-line @typescript-eslint/no-unsafe-call
const _: Set<string> | string = value;
};
const assertNotDateDoesNotExistCheck = (value: Date | string) => {
// @ts-expect-error -- Structural object types such as `Date` can be assignable in TypeScript without matching the runtime brand check.
isAssert.not.date(value); // eslint-disable-line @typescript-eslint/no-unsafe-call
const _: Date | string = value;
};
// Suppress unused variable warnings
nanCheck(42);
finiteNumberCheck(42);
nonNegativeNumberCheck(42);
positiveIntegerCheck(42);
negativeIntegerCheck(-1);
nonNegativeIntegerCheck(0);
infiniteCheck(Number.POSITIVE_INFINITY);
integerCheck(1);
safeIntegerCheck(1);
evenIntegerCheck(2);
oddIntegerCheck(1);
positiveNumberCheck(1);
negativeNumberCheck(-1);
validLengthCheck(0);
integerUnknownCheck(1);
positiveIntegerUnknownCheck(1);
integerMixedUnionCheck(1);
positiveNumberMixedUnionCheck(1);
chainedNumericGuardCheck(1);
distinctNumericBrandsStayDistinct(42 as PositiveInteger, -1 as NegativeInteger, 0 as ValidLength);
assertNotUndefinedCheck('🦄');
assertNotUndefinedUnknownCheck('🦄');
assertNotUndefinedGenericCheck<string | undefined>('🦄');
assertNotNullUnknownCheck('🦄');
assertNotNullOrUndefinedCheck('🦄');
assertNotNullOrUndefinedUnknownCheck('🦄');
assertNotStringCheck(1);
assertNotStringUnknownCheck(1);
assertNotStringGenericCheck<string | number>(1);
assertNotBooleanUnknownCheck(1);
assertNotSymbolUnknownCheck(1);
assertNotBigintUnknownCheck(1);
assertNotPrimitiveUnknownCheck({});
assertNotPrimitiveGenericCheck<string | {unicorn: true}>({unicorn: true});
assertNotNamedUndefinedExportCheck(0);
assertNotNamedNullOrUndefinedUnknownExportCheck('🦄');
assertNotNamedStringExportCheck(1);
assertNotNamedStringUnknownExportCheck(1);
assertNotNamedPrimitiveUnknownExportCheck({});
assertNotCallableDoesNotExistCheck('🦄');
assertNotNumberDoesNotExistCheck(Number.NaN);
assertNotIntegerDoesNotExistCheck(1.5);
assertNotObjectDoesNotExistCheck('🦄');
assertNotBlobDoesNotExistCheck('🦄');
assertNotMapDoesNotExistCheck('🦄');
assertNotSetDoesNotExistCheck('🦄');
assertNotDateDoesNotExistCheck('🦄');

View file

@ -1,28 +1,12 @@
{
"extends": "@sindresorhus/tsconfig",
"compilerOptions": {
"outDir": "dist",
"target": "es2015",
"module": "commonjs",
"moduleResolution": "node",
"declaration": true,
"skipLibCheck": true,
"pretty": true,
"newLine": "lf",
"lib": [
"dom",
"es2015",
"es2017.sharedmemory"
],
"stripInternal": true,
"strict": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"esModuleInterop": true
"types": ["node"],
"rootDir": "source",
"allowImportingTsExtensions": true,
"rewriteRelativeImportExtensions": true
},
"exclude": [
"node_modules",
"dist"
]
"include": [
"source"
],
}

View file

@ -1,3 +0,0 @@
{
"extends": "tslint-xo"
}