Skip to content

Commit

Permalink
Add Float type (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahsnider committed Dec 1, 2021
1 parent 043b732 commit 7770489
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
2 changes: 2 additions & 0 deletions index.d.ts
Expand Up @@ -42,9 +42,11 @@ export {
NegativeInfinity,
Finite,
Integer,
Float,
Negative,
NonNegative,
NegativeInteger,
NegativeFloat,
NonNegativeInteger,
} from './source/numeric';

Expand Down
2 changes: 2 additions & 0 deletions readme.md
Expand Up @@ -132,9 +132,11 @@ Click the type names for complete docs.
- [`NegativeInfinity`](source/numeric.d.ts) - Matches the hidden `-Infinity` type.
- [`Finite`](source/numeric.d.ts) - A finite `number`.
- [`Integer`](source/numeric.d.ts) - A `number` that is an integer.
- [`Float`](source/numeric.d.ts) - A `number` that is not an integer.
- [`Negative`](source/numeric.d.ts) - A negative `number`/`bigint` (`-∞ < x < 0`)
- [`NonNegative`](source/numeric.d.ts) - A non-negative `number`/`bigint` (`0 <= x < ∞`).
- [`NegativeInteger`](source/numeric.d.ts) - A negative (`-∞ < x < 0`) `number` that is an integer.
- [`NegativeFloat`](source/numeric.d.ts) - A negative (`-∞ < x < 0`) `number` that is not an integer.
- [`NonNegativeInteger`](source/numeric.d.ts) - A non-negative (`0 <= x < ∞`) `number` that is an integer.

### Template literal types
Expand Down
32 changes: 32 additions & 0 deletions source/numeric.d.ts
Expand Up @@ -67,6 +67,25 @@ declare function setYear<T extends number>(length: Integer<T>): void;
// Because T is a number and not a string we can effectively use this to filter out any numbers containing decimal points
export type Integer<T extends number> = `${T}` extends `${bigint}` ? T : never;

/**
A `number` that is not an integer.
You can't pass a `bigint` as they are already guaranteed to be integers.
Use-case: Validating and documenting parameters.
@example
```
import {Float} from 'type-fest';
declare function setPercentage<T extends number>(length: Float<T>): void;
```
@see Integer
@category Utilities
*/
export type Float<T extends number> = T extends Integer<T> ? never : T;

/**
A negative `number`/`bigint` (`-∞ < x < 0`)
Expand Down Expand Up @@ -94,6 +113,19 @@ Use-case: Validating and documenting parameters.
*/
export type NegativeInteger<T extends number> = Negative<Integer<T>>;

/**
A negative (`-∞ < x < 0`) `number` that is not an integer.
Equivalent to `Negative<Float<T>>`.
Use-case: Validating and documenting parameters.
@see Negative
@see Float
@category Utilities
*/
export type NegativeFloat<T extends number> = Negative<Float<T>>;

/**
A non-negative `number`/`bigint` (`0 <= x < ∞`).
Expand Down
29 changes: 28 additions & 1 deletion test-d/numeric.ts
@@ -1,5 +1,16 @@
import {expectType} from 'tsd';
import {Finite, Integer, Negative, NegativeInfinity, NegativeInteger, NonNegative, NonNegativeInteger, PositiveInfinity} from '../index';
import {
Finite,
Float,
Integer,
Negative,
NegativeFloat,
NegativeInfinity,
NegativeInteger,
NonNegative,
NonNegativeInteger,
PositiveInfinity,
} from '../index';

// Finite
declare const infinity: Finite<PositiveInfinity | NegativeInfinity>;
Expand All @@ -19,6 +30,17 @@ expectType<never>(integerMixed); // This may be undesired behavior
expectType<never>(nonInteger);
expectType<never>(infinityInteger);

// Float
declare const float: Float<1.5>;
declare const floatMixed: Float<1 | 1.5>;
declare const nonFloat: Float<1>;
declare const infinityFloat: Float<PositiveInfinity | NegativeInfinity>;

expectType<1.5>(float);
expectType<1.5>(floatMixed);
expectType<never>(nonFloat);
expectType<PositiveInfinity | NegativeInfinity>(infinityFloat); // According to Number.isInteger

// Negative
declare const negative: Negative<-1 | -1n | 0 | 0n | 1 | 1n>;

Expand All @@ -29,6 +51,11 @@ declare const negativeInteger: NegativeInteger<-1 | 0 | 1>;

expectType<-1>(negativeInteger);

// NegativeFloat
declare const negativeFloat: NegativeFloat<-1.5 | -1 | 0 | 1 | 1.5>;

expectType<-1.5>(negativeFloat);

// NonNegative
declare const nonNegative: NonNegative<-1 | -1n | 0 | 0n | 1 | 1n>;

Expand Down

0 comments on commit 7770489

Please sign in to comment.