type-fest 4.15.0 → 4.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +2 -0
- package/package.json +1 -1
- package/readme.md +18 -0
- package/source/is-float.d.ts +33 -0
- package/source/is-integer.d.ts +48 -0
- package/source/numeric.d.ts +39 -4
- package/source/single-key-object.d.ts +1 -1
package/index.d.ts
CHANGED
|
@@ -98,6 +98,8 @@ export type {HasReadonlyKeys} from './source/has-readonly-keys';
|
|
|
98
98
|
export type {WritableKeysOf} from './source/writable-keys-of';
|
|
99
99
|
export type {HasWritableKeys} from './source/has-writable-keys';
|
|
100
100
|
export type {Spread} from './source/spread';
|
|
101
|
+
export type {IsInteger} from './source/is-integer';
|
|
102
|
+
export type {IsFloat} from './source/is-float';
|
|
101
103
|
export type {TupleToUnion} from './source/tuple-to-union';
|
|
102
104
|
export type {IntRange} from './source/int-range';
|
|
103
105
|
export type {IsEqual} from './source/is-equal';
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -49,6 +49,22 @@
|
|
|
49
49
|
</a>
|
|
50
50
|
<br>
|
|
51
51
|
<br>
|
|
52
|
+
<br>
|
|
53
|
+
<a href="https://logto.io/?ref=sindre">
|
|
54
|
+
<div>
|
|
55
|
+
<picture>
|
|
56
|
+
<source width="200" media="(prefers-color-scheme: dark)" srcset="https://sindresorhus.com/assets/thanks/logto-logo-dark.svg?x">
|
|
57
|
+
<source width="200" media="(prefers-color-scheme: light)" srcset="https://sindresorhus.com/assets/thanks/logto-logo-light.svg?x">
|
|
58
|
+
<img width="200" src="https://sindresorhus.com/assets/thanks/logto-logo-light.svg?x" alt="Logto logo">
|
|
59
|
+
</picture>
|
|
60
|
+
</div>
|
|
61
|
+
<b>The better identity infrastructure for developers</b>
|
|
62
|
+
<div>
|
|
63
|
+
<sup>Logto is an open-source Auth0 alternative designed for every app.</sup>
|
|
64
|
+
</div>
|
|
65
|
+
</a>
|
|
66
|
+
<br>
|
|
67
|
+
<br>
|
|
52
68
|
</p>
|
|
53
69
|
</div>
|
|
54
70
|
<br>
|
|
@@ -269,6 +285,8 @@ type ShouldBeNever = IfAny<'not any', 'not never', 'never'>;
|
|
|
269
285
|
- [`NegativeInteger`](source/numeric.d.ts) - A negative (`-∞ < x < 0`) `number` that is an integer.
|
|
270
286
|
- [`NonNegativeInteger`](source/numeric.d.ts) - A non-negative (`0 <= x < ∞`) `number` that is an integer.
|
|
271
287
|
- [`IsNegative`](source/numeric.d.ts) - Returns a boolean for whether the given number is a negative number.
|
|
288
|
+
- [`IsFloat`](source/is-float.d.ts) - Returns a boolean for whether the given number is a float, like `1.5` or `-1.5`.
|
|
289
|
+
- [`IsInteger`](source/is-integer.d.ts) - Returns a boolean for whether the given number is a integer, like `-5`, `1.0` or `100`.
|
|
272
290
|
- [`GreaterThan`](source/greater-than.d.ts) - Returns a boolean for whether a given number is greater than another number.
|
|
273
291
|
- [`GreaterThanOrEqual`](source/greater-than-or-equal.d.ts) - Returns a boolean for whether a given number is greater than or equal to another number.
|
|
274
292
|
- [`LessThan`](source/less-than.d.ts) - Returns a boolean for whether a given number is less than another number.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type {Zero} from './numeric';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Returns a boolean for whether the given number is a float, like `1.5` or `-1.5`.
|
|
5
|
+
|
|
6
|
+
It returns `false` for `Infinity`.
|
|
7
|
+
|
|
8
|
+
Use-case:
|
|
9
|
+
- If you want to make a conditional branch based on the result of whether a number is a float or not.
|
|
10
|
+
|
|
11
|
+
@example
|
|
12
|
+
```
|
|
13
|
+
type Float = IsFloat<1.5>;
|
|
14
|
+
//=> true
|
|
15
|
+
|
|
16
|
+
type IntegerWithDecimal = IsInteger<1.0>;
|
|
17
|
+
//=> false
|
|
18
|
+
|
|
19
|
+
type NegativeFloat = IsInteger<-1.5>;
|
|
20
|
+
//=> true
|
|
21
|
+
|
|
22
|
+
type Infinity_ = IsInteger<Infinity>;
|
|
23
|
+
//=> false
|
|
24
|
+
```
|
|
25
|
+
*/
|
|
26
|
+
export type IsFloat<T> =
|
|
27
|
+
T extends number
|
|
28
|
+
? `${T}` extends `${infer _Sign extends '' | '-'}${number}.${infer Decimal extends number}`
|
|
29
|
+
? Decimal extends Zero
|
|
30
|
+
? false
|
|
31
|
+
: true
|
|
32
|
+
: false
|
|
33
|
+
: false;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type {Not} from './internal';
|
|
2
|
+
import type {IsFloat} from './is-float';
|
|
3
|
+
import type {PositiveInfinity, NegativeInfinity} from './numeric';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
Returns a boolean for whether the given number is a integer, like `-5`, `1.0` or `100`.
|
|
7
|
+
|
|
8
|
+
Like [`Number#IsInteger()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/IsInteger) but for types.
|
|
9
|
+
|
|
10
|
+
Use-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
|
+
```
|
|
15
|
+
type Integer = IsInteger<1>;
|
|
16
|
+
//=> true
|
|
17
|
+
|
|
18
|
+
type IntegerWithDecimal = IsInteger<1.0>;
|
|
19
|
+
//=> true
|
|
20
|
+
|
|
21
|
+
type NegativeInteger = IsInteger<-1>;
|
|
22
|
+
//=> true
|
|
23
|
+
|
|
24
|
+
type Float = IsInteger<1.5>;
|
|
25
|
+
//=> false
|
|
26
|
+
|
|
27
|
+
// Supports non-decimal numbers
|
|
28
|
+
|
|
29
|
+
type OctalInteger: IsInteger<0o10>;
|
|
30
|
+
//=> true
|
|
31
|
+
|
|
32
|
+
type BinaryInteger: IsInteger<0b10>;
|
|
33
|
+
//=> true
|
|
34
|
+
|
|
35
|
+
type HexadecimalInteger: IsInteger<0x10>;
|
|
36
|
+
//=> true
|
|
37
|
+
```
|
|
38
|
+
*/
|
|
39
|
+
export type IsInteger<T> =
|
|
40
|
+
T 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;
|
package/source/numeric.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
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
|
|
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
|
|
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.
|