type Numeric = number | bigint; type Zero = 0 | 0n; /** Matches the hidden `Infinity` type. Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/32277) if you want to have this type as a built-in in TypeScript. @see NegativeInfinity @category Numeric */ // See https://github.com/microsoft/TypeScript/issues/31752 // eslint-disable-next-line @typescript-eslint/no-loss-of-precision export type PositiveInfinity = 1e999; /** Matches the hidden `-Infinity` type. Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/32277) if you want to have this type as a built-in in TypeScript. @see PositiveInfinity @category Numeric */ // See https://github.com/microsoft/TypeScript/issues/31752 // eslint-disable-next-line @typescript-eslint/no-loss-of-precision export type NegativeInfinity = -1e999; /** A finite `number`. You can't pass a `bigint` as they are already guaranteed to be finite. Use-case: Validating and documenting parameters. Note: This can't detect `NaN`, please upvote [this issue](https://github.com/microsoft/TypeScript/issues/28682) if you want to have this type as a built-in in TypeScript. @example ``` import type {Finite} from 'type-fest'; declare function setScore(length: Finite): void; ``` @category Numeric */ export type Finite = T extends PositiveInfinity | NegativeInfinity ? never : T; /** A `number` that is an integer. You can't pass a `bigint` as they are already guaranteed to be integers. Use-case: Validating and documenting parameters. @example ``` import type {Integer} from 'type-fest'; declare function setYear(length: Integer): void; ``` @see NegativeInteger @see NonNegativeInteger @category Numeric */ // `${bigint}` is a type that matches a valid bigint literal without the `n` (ex. 1, 0b1, 0o1, 0x1) // 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 `${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 type {Float} from 'type-fest'; declare function setPercentage(length: Float): void; ``` @see Integer @category Numeric */ export type Float = T extends Integer ? never : T; /** A negative (`-∞ < x < 0`) `number` that is not an integer. Equivalent to `Negative>`. Use-case: Validating and documenting parameters. @see Negative @see Float @category Numeric */ export type NegativeFloat = Negative>; /** A negative `number`/`bigint` (`-∞ < x < 0`) Use-case: Validating and documenting parameters. @see NegativeInteger @see NonNegative @category Numeric */ export type Negative = T extends Zero ? never : `${T}` extends `-${string}` ? T : never; /** A negative (`-∞ < x < 0`) `number` that is an integer. Equivalent to `Negative>`. You can't pass a `bigint` as they are already guaranteed to be integers, instead use `Negative`. Use-case: Validating and documenting parameters. @see Negative @see Integer @category Numeric */ export type NegativeInteger = Negative>; /** A non-negative `number`/`bigint` (`0 <= x < ∞`). Use-case: Validating and documenting parameters. @see NonNegativeInteger @see Negative @example ``` import type {NonNegative} from 'type-fest'; declare function setLength(length: NonNegative): void; ``` @category Numeric */ export type NonNegative = T extends Zero ? T : Negative extends never ? T : never; /** A non-negative (`0 <= x < ∞`) `number` that is an integer. Equivalent to `NonNegative>`. You can't pass a `bigint` as they are already guaranteed to be integers, instead use `NonNegative`. Use-case: Validating and documenting parameters. @see NonNegative @see Integer @example ``` import type {NonNegativeInteger} from 'type-fest'; declare function setLength(length: NonNegativeInteger): void; ``` @category Numeric */ export type NonNegativeInteger = NonNegative>;