1
+ import type { IsFloat } from './is-float' ;
2
+ import type { IsInteger } from './is-integer' ;
3
+
1
4
export type Numeric = number | bigint ;
2
5
3
6
type Zero = 0 | 0n ;
@@ -49,10 +52,35 @@ export type Finite<T extends number> = T extends PositiveInfinity | NegativeInfi
49
52
50
53
/**
51
54
A `number` that is an integer.
52
- You can't pass a `bigint` as they are already guaranteed to be integers.
53
55
54
56
Use-case: Validating and documenting parameters.
55
57
58
+ @example
59
+ ```
60
+ type Integer = Integer<1>;
61
+ //=> 1
62
+
63
+ type IntegerWithDecimal = Integer<1.0>;
64
+ //=> 1
65
+
66
+ type NegativeInteger = Integer<-1>;
67
+ //=> -1
68
+
69
+ type Float = Integer<1.5>;
70
+ //=> never
71
+
72
+ // Supports non-decimal numbers
73
+
74
+ type OctalInteger: Integer<0o10>;
75
+ //=> 0o10
76
+
77
+ type BinaryInteger: Integer<0b10>;
78
+ //=> 0b10
79
+
80
+ type HexadecimalInteger: Integer<0x10>;
81
+ //=> 0x10
82
+ ```
83
+
56
84
@example
57
85
```
58
86
import type {Integer} from 'type-fest';
@@ -67,14 +95,18 @@ declare function setYear<T extends number>(length: Integer<T>): void;
67
95
*/
68
96
// `${bigint}` is a type that matches a valid bigint literal without the `n` (ex. 1, 0b1, 0o1, 0x1)
69
97
// Because T is a number and not a string we can effectively use this to filter out any numbers containing decimal points
70
- export type Integer < T extends number > = `${T } ` extends `${bigint } ` ? T : never ;
98
+ export type Integer < T > =
99
+ T extends unknown // To distributive type
100
+ ? IsInteger < T > extends true ? T : never
101
+ : never ; // Never happens
71
102
72
103
/**
73
104
A `number` that is not an integer.
74
- You can't pass a `bigint` as they are already guaranteed to be integers.
75
105
76
106
Use-case: Validating and documenting parameters.
77
107
108
+ It does not accept `Infinity`.
109
+
78
110
@example
79
111
```
80
112
import type {Float} from 'type-fest';
@@ -86,7 +118,10 @@ declare function setPercentage<T extends number>(length: Float<T>): void;
86
118
87
119
@category Numeric
88
120
*/
89
- export type Float < T extends number > = T extends Integer < T > ? never : T ;
121
+ export type Float < T > =
122
+ T extends unknown // To distributive type
123
+ ? IsFloat < T > extends true ? T : never
124
+ : never ; // Never happens
90
125
91
126
/**
92
127
A negative (`-∞ < x < 0`) `number` that is not an integer.
0 commit comments