diff --git a/src/predicate/factory/range.ts b/src/predicate/factory/range.ts index 6a02a9e..1e399c5 100644 --- a/src/predicate/factory/range.ts +++ b/src/predicate/factory/range.ts @@ -1,6 +1,10 @@ import type { TypePredicateFn } from '../../types/functions.js'; -export const range = (min: Type, max: Type): TypePredicateFn => { +export function range(min: number, max: number): TypePredicateFn; + +export function range(min: bigint, max: bigint): TypePredicateFn; + +export function range(min: number | bigint, max: number | bigint): TypePredicateFn { if (min === Number.NEGATIVE_INFINITY && max === Number.POSITIVE_INFINITY) { throw new RangeError('min and max cannot be -Infinity and Infinity'); } @@ -13,6 +17,6 @@ export const range = (min: Type, max: Type): TypeP throw new RangeError('min must be less than max'); } - return (input: unknown): input is Type => + return (input: unknown): input is number | bigint => (typeof input === 'number' || typeof input === 'bigint') && input >= min && input <= max; -}; +} diff --git a/test/predicate/factory/range.test-d.ts b/test/predicate/factory/range.test-d.ts new file mode 100644 index 0000000..a2dc0da --- /dev/null +++ b/test/predicate/factory/range.test-d.ts @@ -0,0 +1,12 @@ +import { describe, expectTypeOf, it } from 'vitest'; + +import { range } from '../../../src/predicate/factory/range.js'; + +import type { TypePredicateFn } from '../../../src/types/functions.js'; + +describe('range()', () => { + it('Returns a type predicate function', () => { + expectTypeOf(range(1, 100)).toEqualTypeOf>(); + expectTypeOf(range(1n, 100n)).toEqualTypeOf>(); + }); +});