UNPKG

684 BTypeScriptView Raw
1import type {Zero} from './numeric';
2
3/**
4Returns a boolean for whether the given number is a float, like `1.5` or `-1.5`.
5
6It returns `false` for `Infinity`.
7
8Use-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```
13type Float = IsFloat<1.5>;
14//=> true
15
16type IntegerWithDecimal = IsInteger<1.0>;
17//=> false
18
19type NegativeFloat = IsInteger<-1.5>;
20//=> true
21
22type Infinity_ = IsInteger<Infinity>;
23//=> false
24```
25*/
26export type IsFloat<T> =
27T 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;