type-fest 2.6.0 → 2.7.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 +10 -0
- package/package.json +1 -1
- package/readme.md +9 -0
- package/source/numeric.d.ts +136 -0
package/index.d.ts
CHANGED
|
@@ -37,6 +37,16 @@ export {SetReturnType} from './source/set-return-type';
|
|
|
37
37
|
export {Asyncify} from './source/asyncify';
|
|
38
38
|
export {Simplify} from './source/simplify';
|
|
39
39
|
export {Jsonify} from './source/jsonify';
|
|
40
|
+
export {
|
|
41
|
+
PositiveInfinity,
|
|
42
|
+
NegativeInfinity,
|
|
43
|
+
Finite,
|
|
44
|
+
Integer,
|
|
45
|
+
Negative,
|
|
46
|
+
NonNegative,
|
|
47
|
+
NegativeInteger,
|
|
48
|
+
NonNegativeInteger,
|
|
49
|
+
} from './source/numeric';
|
|
40
50
|
|
|
41
51
|
// Template literal types
|
|
42
52
|
export {CamelCase} from './source/camel-case';
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -128,6 +128,14 @@ Click the type names for complete docs.
|
|
|
128
128
|
- [`Includes`](source/includes.d.ts) - Returns a boolean for whether the given array includes the given item.
|
|
129
129
|
- [`Simplify`](source/simplify.d.ts) - Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
|
|
130
130
|
- [`Jsonify`](source/jsonify.d.ts) - Transform a type to one that is assignable to the `JsonValue` type.
|
|
131
|
+
- [`PositiveInfinity`](source/numeric-range.d.ts) - Matches the hidden `Infinity` type.
|
|
132
|
+
- [`NegativeInfinity`](source/numeric-range.d.ts) - Matches the hidden `-Infinity` type.
|
|
133
|
+
- [`Finite`](source/numeric-range.d.ts) - A finite `number`.
|
|
134
|
+
- [`Integer`](source/numeric-range.d.ts) - A `number` that is an integer.
|
|
135
|
+
- [`Negative`](source/numeric-range.d.ts) - A negative `number`/`bigint` (`-∞ < x < 0`)
|
|
136
|
+
- [`NonNegative`](source/numeric-range.d.ts) - A non-negative `number`/`bigint` (`0 <= x < ∞`).
|
|
137
|
+
- [`NegativeInteger`](source/numeric-range.d.ts) - A negative (`-∞ < x < 0`) `number` that is an integer.
|
|
138
|
+
- [`NonNegativeInteger`](source/numeric-range.d.ts) - A non-negative (`0 <= x < ∞`) `number` that is an integer.
|
|
131
139
|
|
|
132
140
|
### Template literal types
|
|
133
141
|
|
|
@@ -166,6 +174,7 @@ Click the type names for complete docs.
|
|
|
166
174
|
- [`Dictionary`](https://github.com/sindresorhus/type-fest/issues/33) - You only save a few characters (`Dictionary<number>` vs `Record<string, number>`) from [`Record`](https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type), which is more flexible and well-known. Also, you shouldn't use an object as a dictionary. We have `Map` in JavaScript now.
|
|
167
175
|
- [`ExtractProperties` and `ExtractMethods`](https://github.com/sindresorhus/type-fest/pull/4) - The types violate the single responsibility principle. Instead, refine your types into more granular type hierarchies.
|
|
168
176
|
- [`Url2Json`](https://github.com/sindresorhus/type-fest/pull/262) - Inferring search parameters from a URL string is a cute idea, but not very useful in practice, since search parameters are usually dynamic and defined separately.
|
|
177
|
+
- [`Nullish`](https://github.com/sindresorhus/type-fest/pull/318) - The type only saves a couple of characters, not everyone knows what “nullish” means, and I'm also trying to [get away from `null`](https://github.com/sindresorhus/meta/discussions/7).
|
|
169
178
|
|
|
170
179
|
## Alternative type names
|
|
171
180
|
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
type Numeric = number | bigint;
|
|
2
|
+
|
|
3
|
+
type Zero = 0 | 0n;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
Matches the hidden `Infinity` type.
|
|
7
|
+
|
|
8
|
+
Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/32277) if you want to have this type as a built-in in TypeScript.
|
|
9
|
+
|
|
10
|
+
@see NegativeInfinity
|
|
11
|
+
|
|
12
|
+
@category Basic
|
|
13
|
+
*/
|
|
14
|
+
// See https://github.com/microsoft/TypeScript/issues/31752
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-loss-of-precision
|
|
16
|
+
export type PositiveInfinity = 1e999;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
Matches the hidden `-Infinity` type.
|
|
20
|
+
|
|
21
|
+
Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/32277) if you want to have this type as a built-in in TypeScript.
|
|
22
|
+
|
|
23
|
+
@see PositiveInfinity
|
|
24
|
+
|
|
25
|
+
@category Basic
|
|
26
|
+
*/
|
|
27
|
+
// See https://github.com/microsoft/TypeScript/issues/31752
|
|
28
|
+
// eslint-disable-next-line @typescript-eslint/no-loss-of-precision
|
|
29
|
+
export type NegativeInfinity = -1e999;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
A finite `number`.
|
|
33
|
+
You can't pass a `bigint` as they are already guaranteed to be finite.
|
|
34
|
+
|
|
35
|
+
Use-case: Validating and documenting parameters.
|
|
36
|
+
|
|
37
|
+
@example
|
|
38
|
+
```
|
|
39
|
+
import {Finite} from 'type-fest';
|
|
40
|
+
|
|
41
|
+
declare function setScore<T extends number>(length: Finite<T>): void;
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
@category Utilities
|
|
45
|
+
*/
|
|
46
|
+
export type Finite<T extends number> = T extends PositiveInfinity | NegativeInfinity ? never : T;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
A `number` that is an integer.
|
|
50
|
+
You can't pass a `bigint` as they are already guaranteed to be integers.
|
|
51
|
+
|
|
52
|
+
Use-case: Validating and documenting parameters.
|
|
53
|
+
|
|
54
|
+
@example
|
|
55
|
+
```
|
|
56
|
+
import {Integer} from 'type-fest';
|
|
57
|
+
|
|
58
|
+
declare function setYear<T extends number>(length: Integer<T>): void;
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
@see NegativeInteger
|
|
62
|
+
@see NonNegativeInteger
|
|
63
|
+
|
|
64
|
+
@category Utilities
|
|
65
|
+
*/
|
|
66
|
+
// `${bigint}` is a type that matches a valid bigint literal without the `n` (ex. 1, 0b1, 0o1, 0x1)
|
|
67
|
+
// Because T is a number and not a string we can effectively use this to filter out any numbers containing decimal points
|
|
68
|
+
export type Integer<T extends number> = `${T}` extends `${bigint}` ? T : never;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
A negative `number`/`bigint` (`-∞ < x < 0`)
|
|
72
|
+
|
|
73
|
+
Use-case: Validating and documenting parameters.
|
|
74
|
+
|
|
75
|
+
@see NegativeInteger
|
|
76
|
+
@see NonNegative
|
|
77
|
+
|
|
78
|
+
@category Utilities
|
|
79
|
+
*/
|
|
80
|
+
export type Negative<T extends Numeric> = T extends Zero ? never : `${T}` extends `-${string}` ? T : never;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
A negative (`-∞ < x < 0`) `number` that is an integer.
|
|
84
|
+
Equivalent to `Negative<Integer<T>>`.
|
|
85
|
+
|
|
86
|
+
You can't pass a `bigint` as they are already guaranteed to be integers, instead use `Negative<T>`.
|
|
87
|
+
|
|
88
|
+
Use-case: Validating and documenting parameters.
|
|
89
|
+
|
|
90
|
+
@see Negative
|
|
91
|
+
@see Integer
|
|
92
|
+
|
|
93
|
+
@category Utilities
|
|
94
|
+
*/
|
|
95
|
+
export type NegativeInteger<T extends number> = Negative<Integer<T>>;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
A non-negative `number`/`bigint` (`0 <= x < ∞`).
|
|
99
|
+
|
|
100
|
+
Use-case: Validating and documenting parameters.
|
|
101
|
+
|
|
102
|
+
@see NonNegativeInteger
|
|
103
|
+
@see Negative
|
|
104
|
+
|
|
105
|
+
@example
|
|
106
|
+
```
|
|
107
|
+
import {NonNegative} from 'type-fest';
|
|
108
|
+
|
|
109
|
+
declare function setLength<T extends number>(length: NonNegative<T>): void;
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
@category Utilities
|
|
113
|
+
*/
|
|
114
|
+
export type NonNegative<T extends Numeric> = T extends Zero ? T : Negative<T> extends never ? T : never;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
A non-negative (`0 <= x < ∞`) `number` that is an integer.
|
|
118
|
+
Equivalent to `NonNegative<Integer<T>>`.
|
|
119
|
+
|
|
120
|
+
You can't pass a `bigint` as they are already guaranteed to be integers, instead use `NonNegative<T>`.
|
|
121
|
+
|
|
122
|
+
Use-case: Validating and documenting parameters.
|
|
123
|
+
|
|
124
|
+
@see NonNegative
|
|
125
|
+
@see Integer
|
|
126
|
+
|
|
127
|
+
@example
|
|
128
|
+
```
|
|
129
|
+
import {NonNegativeInteger} from 'type-fest';
|
|
130
|
+
|
|
131
|
+
declare function setLength<T extends number>(length: NonNegativeInteger<T>): void;
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
@category Utilities
|
|
135
|
+
*/
|
|
136
|
+
export type NonNegativeInteger<T extends number> = NonNegative<Integer<T>>;
|