type-fest
Advanced tools
| 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; |
| import type {Not} from './internal'; | ||
| import type {IsFloat} from './is-float'; | ||
| import type {PositiveInfinity, NegativeInfinity} from './numeric'; | ||
| /** | ||
| Returns a boolean for whether the given number is a integer, like `-5`, `1.0` or `100`. | ||
| Like [`Number#IsInteger()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/IsInteger) but for types. | ||
| Use-case: | ||
| - If you want to make a conditional branch based on the result of whether a number is a intrger or not. | ||
| @example | ||
| ``` | ||
| type Integer = IsInteger<1>; | ||
| //=> true | ||
| type IntegerWithDecimal = IsInteger<1.0>; | ||
| //=> true | ||
| type NegativeInteger = IsInteger<-1>; | ||
| //=> true | ||
| type Float = IsInteger<1.5>; | ||
| //=> false | ||
| // Supports non-decimal numbers | ||
| type OctalInteger: IsInteger<0o10>; | ||
| //=> true | ||
| type BinaryInteger: IsInteger<0b10>; | ||
| //=> true | ||
| type HexadecimalInteger: IsInteger<0x10>; | ||
| //=> true | ||
| ``` | ||
| */ | ||
| export type IsInteger<T> = | ||
| T extends bigint | ||
| ? true | ||
| : T extends number | ||
| ? number extends T | ||
| ? false | ||
| : T extends PositiveInfinity | NegativeInfinity | ||
| ? false | ||
| : Not<IsFloat<T>> | ||
| : false; |
+2
-0
@@ -101,2 +101,4 @@ // Basic | ||
| export type {Spread} from './source/spread'; | ||
| export type {IsInteger} from './source/is-integer'; | ||
| export type {IsFloat} from './source/is-float'; | ||
| export type {TupleToUnion} from './source/tuple-to-union'; | ||
@@ -103,0 +105,0 @@ export type {IntRange} from './source/int-range'; |
+1
-1
| { | ||
| "name": "type-fest", | ||
| "version": "4.15.0", | ||
| "version": "4.16.0", | ||
| "description": "A collection of essential TypeScript types", | ||
@@ -5,0 +5,0 @@ "license": "(MIT OR CC0-1.0)", |
+18
-0
@@ -52,2 +52,18 @@ <div align="center"> | ||
| <br> | ||
| <br> | ||
| <a href="https://logto.io/?ref=sindre"> | ||
| <div> | ||
| <picture> | ||
| <source width="200" media="(prefers-color-scheme: dark)" srcset="https://sindresorhus.com/assets/thanks/logto-logo-dark.svg?x"> | ||
| <source width="200" media="(prefers-color-scheme: light)" srcset="https://sindresorhus.com/assets/thanks/logto-logo-light.svg?x"> | ||
| <img width="200" src="https://sindresorhus.com/assets/thanks/logto-logo-light.svg?x" alt="Logto logo"> | ||
| </picture> | ||
| </div> | ||
| <b>The better identity infrastructure for developers</b> | ||
| <div> | ||
| <sup>Logto is an open-source Auth0 alternative designed for every app.</sup> | ||
| </div> | ||
| </a> | ||
| <br> | ||
| <br> | ||
| </p> | ||
@@ -273,2 +289,4 @@ </div> | ||
| - [`IsNegative`](source/numeric.d.ts) - Returns a boolean for whether the given number is a negative number. | ||
| - [`IsFloat`](source/is-float.d.ts) - Returns a boolean for whether the given number is a float, like `1.5` or `-1.5`. | ||
| - [`IsInteger`](source/is-integer.d.ts) - Returns a boolean for whether the given number is a integer, like `-5`, `1.0` or `100`. | ||
| - [`GreaterThan`](source/greater-than.d.ts) - Returns a boolean for whether a given number is greater than another number. | ||
@@ -275,0 +293,0 @@ - [`GreaterThanOrEqual`](source/greater-than-or-equal.d.ts) - Returns a boolean for whether a given number is greater than or equal to another number. |
+39
-4
@@ -0,1 +1,4 @@ | ||
| import type {IsFloat} from './is-float'; | ||
| import type {IsInteger} from './is-integer'; | ||
| export type Numeric = number | bigint; | ||
@@ -52,3 +55,2 @@ | ||
| A `number` that is an integer. | ||
| You can't pass a `bigint` as they are already guaranteed to be integers. | ||
@@ -59,2 +61,28 @@ Use-case: Validating and documenting parameters. | ||
| ``` | ||
| type Integer = Integer<1>; | ||
| //=> 1 | ||
| type IntegerWithDecimal = Integer<1.0>; | ||
| //=> 1 | ||
| type NegativeInteger = Integer<-1>; | ||
| //=> -1 | ||
| type Float = Integer<1.5>; | ||
| //=> never | ||
| // Supports non-decimal numbers | ||
| type OctalInteger: Integer<0o10>; | ||
| //=> 0o10 | ||
| type BinaryInteger: Integer<0b10>; | ||
| //=> 0b10 | ||
| type HexadecimalInteger: Integer<0x10>; | ||
| //=> 0x10 | ||
| ``` | ||
| @example | ||
| ``` | ||
| import type {Integer} from 'type-fest'; | ||
@@ -72,10 +100,14 @@ | ||
| // 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; | ||
| export type Integer<T> = | ||
| T extends unknown // To distributive type | ||
| ? IsInteger<T> extends true ? T : never | ||
| : never; // Never happens | ||
| /** | ||
| 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. | ||
| It does not accept `Infinity`. | ||
| @example | ||
@@ -92,3 +124,6 @@ ``` | ||
| */ | ||
| export type Float<T extends number> = T extends Integer<T> ? never : T; | ||
| export type Float<T> = | ||
| T extends unknown // To distributive type | ||
| ? IsFloat<T> extends true ? T : never | ||
| : never; // Never happens | ||
@@ -95,0 +130,0 @@ /** |
@@ -1,2 +0,2 @@ | ||
| import type {IfEmptyObject} from '../index'; | ||
| import type {IfEmptyObject} from './if-empty-object'; | ||
| import type {IsUnion} from './internal'; | ||
@@ -3,0 +3,0 @@ |
AI-detected potential code anomaly
Supply chain riskAI has identified unusual behaviors that may pose a security risk.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
AI-detected potential code anomaly
Supply chain riskAI has identified unusual behaviors that may pose a security risk.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
360878
0.91%137
1.48%8696
1.05%990
1.85%