-
-
Notifications
You must be signed in to change notification settings - Fork 679
Expand file tree
/
Copy pathabsolute.d.ts
More file actions
52 lines (38 loc) · 1000 Bytes
/
absolute.d.ts
File metadata and controls
52 lines (38 loc) · 1000 Bytes
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
49
50
51
52
import type {StringToNumber} from './internal/string.d.ts';
/**
Returns the absolute value of the specified number or bigint.
@example
```
import type {Absolute} from 'type-fest';
type A = Absolute<-1>;
//=> 1
type B = Absolute<1>;
//=> 1
type C = Absolute<0>;
//=> 0
type D = Absolute<-1.025>;
//=> 1.025
type E = Absolute<-9999n>;
//=> 9999n
```
Returns back the same type if the input is not a literal type.
@example
```
import type {Absolute} from 'type-fest';
type A = Absolute<number>;
//=> number
type B = Absolute<bigint>;
//=> bigint
type C = Absolute<number | bigint>;
//=> number | bigint
```
@category Numeric
*/
export type Absolute<N extends number | bigint> = N extends bigint // Also, distributes `N`
? `${N}` extends `-${infer Magnitude extends bigint}`
? Magnitude
: N
: `${N}` extends `-${infer Magnitude}` // This doesn't use the `extends number` constraint approach because that fails with the `-Infinity` case
? StringToNumber<Magnitude>
: N;
export {};