-
-
Notifications
You must be signed in to change notification settings - Fork 547
/
is-integer.d.ts
48 lines (37 loc) · 1.05 KB
/
is-integer.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import type {Not} from './internal';
import type {IsFloat} from './is-float';
import type {PositiveInfinity, NegativeInfinity} from './numeric';
/**
Returns a boolean for whether the given number is a integer, like `-5`, `1.0` or `100`.
Like [`Number#IsInteger()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/IsInteger) but for types.
Use-case:
- If you want to make a conditional branch based on the result of whether a number is a intrger or not.
@example
```
type Integer = IsInteger<1>;
//=> true
type IntegerWithDecimal = IsInteger<1.0>;
//=> true
type NegativeInteger = IsInteger<-1>;
//=> true
type Float = IsInteger<1.5>;
//=> false
// Supports non-decimal numbers
type OctalInteger: IsInteger<0o10>;
//=> true
type BinaryInteger: IsInteger<0b10>;
//=> true
type HexadecimalInteger: IsInteger<0x10>;
//=> true
```
*/
export type IsInteger<T> =
T extends bigint
? true
: T extends number
? number extends T
? false
: T extends PositiveInfinity | NegativeInfinity
? false
: Not<IsFloat<T>>
: false;