type-fest/source/is-float.d.ts
Haozheng Li f5b09de767
Add IsInteger and IsFloat, fix Integer and Float handing with edge case (#857)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
2024-04-22 15:22:35 +07:00

34 lines
684 B
TypeScript

import type {Zero} from './numeric';
/**
Returns a boolean for whether the given number is a float, like `1.5` or `-1.5`.
It returns `false` for `Infinity`.
Use-case:
- If you want to make a conditional branch based on the result of whether a number is a float or not.
@example
```
type Float = IsFloat<1.5>;
//=> true
type IntegerWithDecimal = IsInteger<1.0>;
//=> false
type NegativeFloat = IsInteger<-1.5>;
//=> true
type Infinity_ = IsInteger<Infinity>;
//=> false
```
*/
export type IsFloat<T> =
T extends number
? `${T}` extends `${infer _Sign extends '' | '-'}${number}.${infer Decimal extends number}`
? Decimal extends Zero
? false
: true
: false
: false;