Repository: sindresorhus/ow
Pull request #212 (2021-06-05 01:23:16)

Add `ow.bigint`
Resolve: https://github.com/sindresorhus/ow/issues/54

wafuwafu13: I don't know why only node10,12 fails the test because of SyntaxError.
`Bigint` is supported in node10.
sindresorhus: > I don't know why only node10,12 fails the test because of SyntaxError.
Bigint is supported in node10.

It's fine. I'm going to drop Node.js 10 support after merging this PR.

Patch:
diff --git a/source/predicates.ts b/source/predicates.ts
index 5edf6c6..b83aa75 100644
--- a/source/predicates.ts
+++ b/source/predicates.ts
@@ -1,6 +1,7 @@
 import {TypedArray} from 'type-fest';
 import {StringPredicate} from './predicates/string';
 import {NumberPredicate} from './predicates/number';
+import {BigIntPredicate} from './predicates/bigint';
 import {BooleanPredicate} from './predicates/boolean';
 import {Predicate, PredicateOptions} from './predicates/predicate';
 import {ArrayPredicate} from './predicates/array';
@@ -28,6 +29,11 @@ export interface Predicates {
 	*/
 	readonly number: NumberPredicate;
 
+	/**
+	Test the value to be an bigint.
+	*/
+	readonly bigint: BigIntPredicate;
+
 	/**
 	Test the value to be a boolean.
 	*/
@@ -212,6 +218,9 @@ export default <T>(object: T, options?: PredicateOptions): T & Predicates => {
 		number: {
 			get: (): NumberPredicate => new NumberPredicate(options)
 		},
+		bigint: {
+			get: (): BigIntPredicate => new BigIntPredicate(options)
+		},
 		boolean: {
 			get: (): BooleanPredicate => new BooleanPredicate(options)
 		},
@@ -319,6 +328,7 @@ export default <T>(object: T, options?: PredicateOptions): T & Predicates => {
 export {
 	StringPredicate,
 	NumberPredicate,
+	BigIntPredicate,
 	BooleanPredicate,
 	ArrayPredicate,
 	ObjectPredicate,
diff --git a/source/predicates/bigint.ts b/source/predicates/bigint.ts
new file mode 100644
index 0000000..4e60edc
--- /dev/null
+++ b/source/predicates/bigint.ts
@@ -0,0 +1,10 @@
+import {Predicate, PredicateOptions} from './predicate';
+
+export class BigIntPredicate extends Predicate<bigint> {
+	/**
+	@hidden
+	*/
+	constructor(options?: PredicateOptions) {
+		super('bigint', options);
+	}
+}
diff --git a/tsconfig.json b/tsconfig.json
index 1051c8f..5dc8ef5 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -2,9 +2,9 @@
 	"extends": "@sindresorhus/tsconfig",
 	"compilerOptions": {
 		"outDir": "dist",
-		"target": "es2018",
+		"target": "es2020",
 		"lib": [
-			"es2018"
+			"es2020"
 		]
 	},
 	"include": [


Test patch:
diff --git a/test/bigint.ts b/test/bigint.ts
new file mode 100644
index 0000000..592de0d
--- /dev/null
+++ b/test/bigint.ts
@@ -0,0 +1,18 @@
+import test from 'ava';
+import ow from '../source';
+
+test('bigint', t => {
+	t.notThrows(() => {
+		// eslint-disable-next-line node/no-unsupported-features/es-builtins
+		ow(BigInt(9007199254740991), ow.bigint);
+	});
+
+	t.notThrows(() => {
+		// eslint-disable-next-line node/no-unsupported-features/es-syntax
+		ow(9007199254740991n, ow.bigint);
+	});
+
+	t.throws(() => {
+		ow(10, ow.bigint);
+	}, 'Expected argument to be of type `bigint` but received type `number`');
+});
diff --git a/test/types.ts b/test/types.ts
index 9d75d60..99654ea 100644
--- a/test/types.ts
+++ b/test/types.ts
@@ -106,6 +106,9 @@ function typeTests(value: unknown): Array<(() => void)> {
 			const tests: Tests = {
 				array: expect => expect.toBeArray(),
 				arrayBuffer: expect => expect.toEqualTypeOf<ArrayBuffer>(),
+				// @ts-expect-error
+				// eslint-disable-next-line node/no-unsupported-features/es-builtins
+				bigint: expect => expect.toEqualTypeOf<BigInt>(),
 				boolean: expect => expect.toBeBoolean(),
 				buffer: expect => expect.toEqualTypeOf<Buffer>(),
 				dataView: expect => expect.toEqualTypeOf<DataView>(),