Skip to content

Commit 99c2004

Browse files
authored
Merge pull request #179 from webdeveric/fix/range
fix(range): use function overloading for better return type
2 parents 3a72339 + 40a1e9c commit 99c2004

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/predicate/factory/range.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import type { TypePredicateFn } from '../../types/functions.js';
22

3-
export const range = <Type extends number | bigint>(min: Type, max: Type): TypePredicateFn<Type> => {
3+
export function range(min: number, max: number): TypePredicateFn<number>;
4+
5+
export function range(min: bigint, max: bigint): TypePredicateFn<bigint>;
6+
7+
export function range(min: number | bigint, max: number | bigint): TypePredicateFn<number | bigint> {
48
if (min === Number.NEGATIVE_INFINITY && max === Number.POSITIVE_INFINITY) {
59
throw new RangeError('min and max cannot be -Infinity and Infinity');
610
}
@@ -13,6 +17,6 @@ export const range = <Type extends number | bigint>(min: Type, max: Type): TypeP
1317
throw new RangeError('min must be less than max');
1418
}
1519

16-
return (input: unknown): input is Type =>
20+
return (input: unknown): input is number | bigint =>
1721
(typeof input === 'number' || typeof input === 'bigint') && input >= min && input <= max;
18-
};
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { describe, expectTypeOf, it } from 'vitest';
2+
3+
import { range } from '../../../src/predicate/factory/range.js';
4+
5+
import type { TypePredicateFn } from '../../../src/types/functions.js';
6+
7+
describe('range()', () => {
8+
it('Returns a type predicate function', () => {
9+
expectTypeOf(range(1, 100)).toEqualTypeOf<TypePredicateFn<number>>();
10+
expectTypeOf(range(1n, 100n)).toEqualTypeOf<TypePredicateFn<bigint>>();
11+
});
12+
});

0 commit comments

Comments
 (0)