UNPKG

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