Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Float type #330

Merged
merged 2 commits into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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