From dd7e25a7aea6bed60c63cf7286eb8c10def30535 Mon Sep 17 00:00:00 2001 From: Emiya Date: Sun, 7 Apr 2024 14:44:07 +0800 Subject: [PATCH 01/12] `Integer` and `Float`: fix handing with negative number and non-decimal numbers and big number --- source/numeric.d.ts | 38 ++++++++++++++++++++++++++++++++++++-- test-d/numeric.ts | 20 +++++++++++++++----- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/source/numeric.d.ts b/source/numeric.d.ts index e6906dd12..06a82be73 100644 --- a/source/numeric.d.ts +++ b/source/numeric.d.ts @@ -2,6 +2,34 @@ export type Numeric = number | bigint; type Zero = 0 | 0n; +/** +Returns the given number if it is a float, like `1.5` or `-1.5`. +*/ +type IsFloat = `${T}` extends `${number}.${number}` | `-${number}.${number}` ? true : false; + +/** +Returns the given number if it is an integer, like `-5`, `1` or `100`. + +Like [`Number#IsInteger()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/IsInteger) but for types. + +@example +```ts +type Integer = IsInteger<1>; // true +type NegativeInteger = IsInteger<-1>; // true +type Float = IsInteger<1.5>; // false + +// supported non-decimal numbers +type OctalInteger: IsInteger<0o10>; // true +type BinaryInteger: IsInteger<0b10>; // true +type HexadecimalInteger: IsInteger<0x10>; // true +``` +*/ +type IsInteger = +number extends T ? false + : T extends PositiveInfinity | NegativeInfinity ? false + : IsFloat extends true ? false + : T; + /** Matches the hidden `Infinity` type. @@ -67,7 +95,10 @@ declare function setYear(length: Integer): void; */ // `${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; +export type Integer = + T extends unknown // To distributive type + ? IsInteger extends true ? T : never + : never; // Never happens /** A `number` that is not an integer. @@ -86,7 +117,10 @@ declare function setPercentage(length: Float): void; @category Numeric */ -export type Float = T extends Integer ? never : T; +export type Float = +T extends unknown // To distributive type + ? IsFloat extends true ? T : never + : never; // Never happens /** A negative (`-∞ < x < 0`) `number` that is not an integer. diff --git a/test-d/numeric.ts b/test-d/numeric.ts index 75466f5a8..707081a64 100644 --- a/test-d/numeric.ts +++ b/test-d/numeric.ts @@ -21,25 +21,35 @@ expectType<1>(infinityMixed); // Integer declare const integer: Integer<1>; -declare const integerMixed: Integer<1 | 1.5>; +declare const numberType: Integer; +declare const integerMixed: Integer<1 | 1.5 | -1>; +declare const bigInteger: Integer<1e+100>; +declare const octalInteger: Integer<0o10>; +declare const binaryInteger: Integer<0b10>; +declare const hexadecimalInteger: Integer<0x10>; declare const nonInteger: Integer<1.5>; declare const infinityInteger: Integer; expectType<1>(integer); -expectType(integerMixed); // This may be undesired behavior +expectType(numberType); +expectType<1 | -1>(integerMixed); +expectType<1e+100>(bigInteger); +expectType<0o10>(octalInteger); +expectType<0b10>(binaryInteger); +expectType<0x10>(hexadecimalInteger); expectType(nonInteger); expectType(infinityInteger); // Float declare const float: Float<1.5>; -declare const floatMixed: Float<1 | 1.5>; +declare const floatMixed: Float<1 | 1.5 | -1.5>; declare const nonFloat: Float<1>; declare const infinityFloat: Float; expectType<1.5>(float); -expectType<1.5>(floatMixed); +expectType<1.5 | -1.5>(floatMixed); expectType(nonFloat); -expectType(infinityFloat); // According to Number.isInteger +expectType(infinityFloat); // Negative declare const negative: Negative<-1 | -1n | 0 | 0n | 1 | 1n>; From bef665ccefde0f25daa13e696e4d3029b8cae7d1 Mon Sep 17 00:00:00 2001 From: Emiya Date: Sun, 7 Apr 2024 15:22:03 +0800 Subject: [PATCH 02/12] fix handling of integer like 1.0 --- source/numeric.d.ts | 17 ++++++++++++++--- test-d/numeric.ts | 2 ++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/source/numeric.d.ts b/source/numeric.d.ts index 06a82be73..98d58708b 100644 --- a/source/numeric.d.ts +++ b/source/numeric.d.ts @@ -5,7 +5,16 @@ type Zero = 0 | 0n; /** Returns the given number if it is a float, like `1.5` or `-1.5`. */ -type IsFloat = `${T}` extends `${number}.${number}` | `-${number}.${number}` ? true : false; +type IsFloat = + `${T}` extends `${number}.${infer Decimal extends number}` + ? Decimal extends Zero + ? false + : true + : `${T}` extends `-${number}.${infer Decimal extends number}` + ? Decimal extends Zero + ? false + : true + : false; /** Returns the given number if it is an integer, like `-5`, `1` or `100`. @@ -15,6 +24,7 @@ Like [`Number#IsInteger()`](https://developer.mozilla.org/en-US/docs/Web/JavaScr @example ```ts type Integer = IsInteger<1>; // true +type IntegerWithDecimal = IsInteger<1.0>; // true type NegativeInteger = IsInteger<-1>; // true type Float = IsInteger<1.5>; // false @@ -27,8 +37,9 @@ type HexadecimalInteger: IsInteger<0x10>; // true type IsInteger = number extends T ? false : T extends PositiveInfinity | NegativeInfinity ? false - : IsFloat extends true ? false - : T; + : IsFloat extends true + ? false + : true; /** Matches the hidden `Infinity` type. diff --git a/test-d/numeric.ts b/test-d/numeric.ts index 707081a64..0e1c20af2 100644 --- a/test-d/numeric.ts +++ b/test-d/numeric.ts @@ -21,6 +21,7 @@ expectType<1>(infinityMixed); // Integer declare const integer: Integer<1>; +declare const integerWithDecimal: Integer<1.0>; // eslint-disable-line unicorn/no-zero-fractions declare const numberType: Integer; declare const integerMixed: Integer<1 | 1.5 | -1>; declare const bigInteger: Integer<1e+100>; @@ -31,6 +32,7 @@ declare const nonInteger: Integer<1.5>; declare const infinityInteger: Integer; expectType<1>(integer); +expectType<1>(integerWithDecimal); expectType(numberType); expectType<1 | -1>(integerMixed); expectType<1e+100>(bigInteger); From 698164194254b4ffbdf2ae164127e2081727f6b1 Mon Sep 17 00:00:00 2001 From: Emiya Date: Sun, 7 Apr 2024 15:27:36 +0800 Subject: [PATCH 03/12] fix --- source/numeric.d.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/source/numeric.d.ts b/source/numeric.d.ts index 98d58708b..2ee12be09 100644 --- a/source/numeric.d.ts +++ b/source/numeric.d.ts @@ -6,15 +6,11 @@ type Zero = 0 | 0n; Returns the given number if it is a float, like `1.5` or `-1.5`. */ type IsFloat = - `${T}` extends `${number}.${infer Decimal extends number}` + `${T}` extends `${infer _Sign extends '' | '-'}${number}.${infer Decimal extends number}` ? Decimal extends Zero ? false : true - : `${T}` extends `-${number}.${infer Decimal extends number}` - ? Decimal extends Zero - ? false - : true - : false; + : false; /** Returns the given number if it is an integer, like `-5`, `1` or `100`. From e2421b60a7b9e5daefde03271fcd402581723b60 Mon Sep 17 00:00:00 2001 From: Emiya Date: Sun, 7 Apr 2024 15:33:31 +0800 Subject: [PATCH 04/12] add example --- source/numeric.d.ts | 47 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/source/numeric.d.ts b/source/numeric.d.ts index 2ee12be09..13bc53a8f 100644 --- a/source/numeric.d.ts +++ b/source/numeric.d.ts @@ -18,16 +18,23 @@ Returns the given number if it is an integer, like `-5`, `1` or `100`. Like [`Number#IsInteger()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/IsInteger) but for types. @example -```ts -type Integer = IsInteger<1>; // true -type IntegerWithDecimal = IsInteger<1.0>; // true -type NegativeInteger = IsInteger<-1>; // true -type Float = IsInteger<1.5>; // false - -// supported non-decimal numbers -type OctalInteger: IsInteger<0o10>; // true -type BinaryInteger: IsInteger<0b10>; // true -type HexadecimalInteger: IsInteger<0x10>; // true +``` +type Integer = IsInteger<1>; +//=> true +type IntegerWithDecimal = IsInteger<1.0>; +//=> true +type NegativeInteger = IsInteger<-1>; +//=> true +type Float = IsInteger<1.5>; +//=> false + +//=> supported non-decimal numbers +type OctalInteger: IsInteger<0o10>; +//=> true +type BinaryInteger: IsInteger<0b10>; +//=> true +type HexadecimalInteger: IsInteger<0x10>; +//=> true ``` */ type IsInteger = @@ -88,6 +95,26 @@ You can't pass a `bigint` as they are already guaranteed to be integers. Use-case: Validating and documenting parameters. +@example +``` +type Integer = Integer<1>; +//=> 1 +type IntegerWithDecimal = Integer<1.0>; +//=> 1 +type NegativeInteger = Integer<-1>; +//=> -1 +type Float = Integer<1.5>; +//=> never + +//=> supported 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'; From 4cfb2a4379635853f9d22208d9b429ce334a6ecc Mon Sep 17 00:00:00 2001 From: Emiya Date: Sun, 7 Apr 2024 15:38:36 +0800 Subject: [PATCH 05/12] chore --- source/numeric.d.ts | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/source/numeric.d.ts b/source/numeric.d.ts index 13bc53a8f..29315d25c 100644 --- a/source/numeric.d.ts +++ b/source/numeric.d.ts @@ -1,3 +1,5 @@ +import type {Not} from './internal'; + export type Numeric = number | bigint; type Zero = 0 | 0n; @@ -6,11 +8,11 @@ type Zero = 0 | 0n; Returns the given number if it is a float, like `1.5` or `-1.5`. */ type IsFloat = - `${T}` extends `${infer _Sign extends '' | '-'}${number}.${infer Decimal extends number}` - ? Decimal extends Zero - ? false - : true - : false; +`${T}` extends `${infer _Sign extends '' | '-'}${number}.${infer Decimal extends number}` + ? Decimal extends Zero + ? false + : true + : false; /** Returns the given number if it is an integer, like `-5`, `1` or `100`. @@ -38,11 +40,11 @@ type HexadecimalInteger: IsInteger<0x10>; ``` */ type IsInteger = -number extends T ? false - : T extends PositiveInfinity | NegativeInfinity ? false - : IsFloat extends true - ? false - : true; +number extends T + ? false + : T extends PositiveInfinity | NegativeInfinity + ? false + : Not>; /** Matches the hidden `Infinity` type. From b8a5dd4c90dbd072f2d8e7ee511a190a7da5ed0b Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Mon, 8 Apr 2024 12:17:18 +0900 Subject: [PATCH 06/12] Update numeric.d.ts --- source/numeric.d.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/source/numeric.d.ts b/source/numeric.d.ts index 29315d25c..25b504740 100644 --- a/source/numeric.d.ts +++ b/source/numeric.d.ts @@ -23,18 +23,24 @@ Like [`Number#IsInteger()`](https://developer.mozilla.org/en-US/docs/Web/JavaScr ``` type Integer = IsInteger<1>; //=> true + type IntegerWithDecimal = IsInteger<1.0>; //=> true + type NegativeInteger = IsInteger<-1>; //=> true + type Float = IsInteger<1.5>; //=> false -//=> supported non-decimal numbers +// Supports non-decimal numbers + type OctalInteger: IsInteger<0o10>; //=> true + type BinaryInteger: IsInteger<0b10>; //=> true + type HexadecimalInteger: IsInteger<0x10>; //=> true ``` @@ -101,18 +107,24 @@ 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 -//=> supported non-decimal numbers +// Supports non-decimal numbers + type OctalInteger: Integer<0o10>; //=> 0o10 + type BinaryInteger: Integer<0b10>; //=> 0b10 + type HexadecimalInteger: Integer<0x10>; //=> 0x10 ``` From 79ae733ab18ae5596f3f5930b8a941b0e33eb302 Mon Sep 17 00:00:00 2001 From: Emiya Date: Wed, 17 Apr 2024 15:02:31 +0800 Subject: [PATCH 07/12] update --- index.d.ts | 2 ++ source/is-float.d.ts | 30 +++++++++++++++++++++++++ source/is-integer.d.ts | 42 ++++++++++++++++++++++++++++++++++ source/numeric.d.ts | 51 ++++-------------------------------------- test-d/is-float.ts | 17 ++++++++++++++ test-d/is-integer.ts | 18 +++++++++++++++ test-d/numeric.ts | 3 +++ 7 files changed, 116 insertions(+), 47 deletions(-) create mode 100644 source/is-float.d.ts create mode 100644 source/is-integer.d.ts create mode 100644 test-d/is-float.ts create mode 100644 test-d/is-integer.ts diff --git a/index.d.ts b/index.d.ts index 5c62cd009..af61223c5 100644 --- a/index.d.ts +++ b/index.d.ts @@ -98,6 +98,8 @@ export type {HasReadonlyKeys} from './source/has-readonly-keys'; export type {WritableKeysOf} from './source/writable-keys-of'; export type {HasWritableKeys} from './source/has-writable-keys'; 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'; export type {IntRange} from './source/int-range'; export type {IsEqual} from './source/is-equal'; diff --git a/source/is-float.d.ts b/source/is-float.d.ts new file mode 100644 index 000000000..5ed1bf8a5 --- /dev/null +++ b/source/is-float.d.ts @@ -0,0 +1,30 @@ +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; +//=> false +``` +*/ +export type IsFloat = +T extends number + ? `${T}` extends `${infer _Sign extends '' | '-'}${number}.${infer Decimal extends number}` + ? Decimal extends Zero + ? false + : true + : false + : false; diff --git a/source/is-integer.d.ts b/source/is-integer.d.ts new file mode 100644 index 000000000..aecf63d56 --- /dev/null +++ b/source/is-integer.d.ts @@ -0,0 +1,42 @@ +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 float, like `-5`, `1` 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 + +//=> supported non-decimal numbers +type OctalInteger: IsInteger<0o10>; +//=> true +type BinaryInteger: IsInteger<0b10>; +//=> true +type HexadecimalInteger: IsInteger<0x10>; +//=> true +``` +*/ +export type IsInteger = +T extends bigint + ? true + : T extends number + ? number extends T + ? false + : T extends PositiveInfinity | NegativeInfinity + ? false + : Not> + : false; diff --git a/source/numeric.d.ts b/source/numeric.d.ts index 29315d25c..ffb668262 100644 --- a/source/numeric.d.ts +++ b/source/numeric.d.ts @@ -1,51 +1,10 @@ -import type {Not} from './internal'; +import type {IsFloat} from './is-float'; +import type {IsInteger} from './is-integer'; export type Numeric = number | bigint; type Zero = 0 | 0n; -/** -Returns the given number if it is a float, like `1.5` or `-1.5`. -*/ -type IsFloat = -`${T}` extends `${infer _Sign extends '' | '-'}${number}.${infer Decimal extends number}` - ? Decimal extends Zero - ? false - : true - : false; - -/** -Returns the given number if it is an integer, like `-5`, `1` or `100`. - -Like [`Number#IsInteger()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/IsInteger) but for types. - -@example -``` -type Integer = IsInteger<1>; -//=> true -type IntegerWithDecimal = IsInteger<1.0>; -//=> true -type NegativeInteger = IsInteger<-1>; -//=> true -type Float = IsInteger<1.5>; -//=> false - -//=> supported non-decimal numbers -type OctalInteger: IsInteger<0o10>; -//=> true -type BinaryInteger: IsInteger<0b10>; -//=> true -type HexadecimalInteger: IsInteger<0x10>; -//=> true -``` -*/ -type IsInteger = -number extends T - ? false - : T extends PositiveInfinity | NegativeInfinity - ? false - : Not>; - /** Matches the hidden `Infinity` type. @@ -93,7 +52,6 @@ export type Finite = T extends PositiveInfinity | NegativeInfi /** 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. @@ -131,14 +89,13 @@ declare function setYear(length: Integer): void; */ // `${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 = +export type Integer = T extends unknown // To distributive type ? IsInteger 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. @@ -153,7 +110,7 @@ declare function setPercentage(length: Float): void; @category Numeric */ -export type Float = +export type Float = T extends unknown // To distributive type ? IsFloat extends true ? T : never : never; // Never happens diff --git a/test-d/is-float.ts b/test-d/is-float.ts new file mode 100644 index 000000000..0ec1cbca4 --- /dev/null +++ b/test-d/is-float.ts @@ -0,0 +1,17 @@ +import {expectType} from 'tsd'; +import type {IsFloat, PositiveInfinity} from '../index'; + +expectType({} as IsFloat<0>); +expectType({} as IsFloat<1>); +expectType({} as IsFloat<1.0>); // eslint-disable-line unicorn/no-zero-fractions +expectType({} as IsFloat<1.5>); +expectType({} as IsFloat<-1>); +expectType({} as IsFloat); +expectType({} as IsFloat<0o10>); +expectType({} as IsFloat<1n>); +expectType({} as IsFloat<0n>); +expectType({} as IsFloat<0b10>); +expectType({} as IsFloat<0x10>); +expectType({} as IsFloat<1e+100>); +expectType({} as IsFloat); +expectType({} as IsFloat); diff --git a/test-d/is-integer.ts b/test-d/is-integer.ts new file mode 100644 index 000000000..39658a1e9 --- /dev/null +++ b/test-d/is-integer.ts @@ -0,0 +1,18 @@ +import {expectType} from 'tsd'; +import type {IsInteger, PositiveInfinity} from '../index'; + +expectType({} as IsInteger<0>); +expectType({} as IsInteger<1>); +expectType({} as IsInteger<1.0>); // eslint-disable-line unicorn/no-zero-fractions +expectType({} as IsInteger<1.5>); +expectType({} as IsInteger<-1>); +expectType({} as IsInteger); +expectType({} as IsInteger<0o10>); +expectType({} as IsInteger<1n>); +expectType({} as IsInteger<0n>); +expectType({} as IsInteger<0b10>); +expectType({} as IsInteger<0x10>); +expectType({} as IsInteger<1e+100>); +expectType({} as IsInteger); +expectType({} as IsInteger); + diff --git a/test-d/numeric.ts b/test-d/numeric.ts index 0e1c20af2..b8718b447 100644 --- a/test-d/numeric.ts +++ b/test-d/numeric.ts @@ -30,6 +30,8 @@ declare const binaryInteger: Integer<0b10>; declare const hexadecimalInteger: Integer<0x10>; declare const nonInteger: Integer<1.5>; declare const infinityInteger: Integer; +const infinityValue = Number.POSITIVE_INFINITY; +declare const infinityInteger2: Integer; expectType<1>(integer); expectType<1>(integerWithDecimal); @@ -41,6 +43,7 @@ expectType<0b10>(binaryInteger); expectType<0x10>(hexadecimalInteger); expectType(nonInteger); expectType(infinityInteger); +expectType(infinityInteger2); // Float declare const float: Float<1.5>; From c099652787dd6670a3fab5fca8369ecbbef7fdcc Mon Sep 17 00:00:00 2001 From: Emiya Date: Mon, 22 Apr 2024 12:29:35 +0800 Subject: [PATCH 08/12] update readme --- readme.md | 2 ++ source/is-integer.d.ts | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 1238a0efc..71cf132bf 100644 --- a/readme.md +++ b/readme.md @@ -269,6 +269,8 @@ type ShouldBeNever = IfAny<'not any', 'not never', 'never'>; - [`NegativeInteger`](source/numeric.d.ts) - A negative (`-∞ < x < 0`) `number` that is an integer. - [`NonNegativeInteger`](source/numeric.d.ts) - A non-negative (`0 <= x < ∞`) `number` that is an integer. - [`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. - [`GreaterThanOrEqual`](source/greater-than-or-equal.d.ts) - Returns a boolean for whether a given number is greater than or equal to another number. - [`LessThan`](source/less-than.d.ts) - Returns a boolean for whether a given number is less than another number. diff --git a/source/is-integer.d.ts b/source/is-integer.d.ts index aecf63d56..6a65c3cb1 100644 --- a/source/is-integer.d.ts +++ b/source/is-integer.d.ts @@ -3,7 +3,7 @@ import type {IsFloat} from './is-float'; import type {PositiveInfinity, NegativeInfinity} from './numeric'; /** -Returns a boolean for whether the given number is a float, like `-5`, `1` or `100`. +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. From 4fcf59ba3e1f28c32ce6a0dfa4338e4dc93f9b71 Mon Sep 17 00:00:00 2001 From: Emiya Date: Mon, 22 Apr 2024 12:31:43 +0800 Subject: [PATCH 09/12] chore --- source/is-float.d.ts | 3 +++ source/is-integer.d.ts | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/source/is-float.d.ts b/source/is-float.d.ts index 5ed1bf8a5..dd3c2723c 100644 --- a/source/is-float.d.ts +++ b/source/is-float.d.ts @@ -12,10 +12,13 @@ Use-case: ``` type Float = IsFloat<1.5>; //=> true + type IntegerWithDecimal = IsInteger<1.0>; //=> false + type NegativeFloat = IsInteger<-1.5>; //=> true + type Infinity_ = IsInteger; //=> false ``` diff --git a/source/is-integer.d.ts b/source/is-integer.d.ts index 6a65c3cb1..b5ec6c5da 100644 --- a/source/is-integer.d.ts +++ b/source/is-integer.d.ts @@ -14,18 +14,23 @@ Use-case: ``` type Integer = IsInteger<1>; //=> true + type IntegerWithDecimal = IsInteger<1.0>; //=> true + type NegativeInteger = IsInteger<-1>; //=> true + type Float = IsInteger<1.5>; //=> false -//=> supported non-decimal numbers +// Supported non-decimal numbers type OctalInteger: IsInteger<0o10>; //=> true + type BinaryInteger: IsInteger<0b10>; //=> true + type HexadecimalInteger: IsInteger<0x10>; //=> true ``` From 2ae795527fb2f6e53f8e7f0fc5ff80adde8b6851 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Mon, 22 Apr 2024 15:01:38 +0700 Subject: [PATCH 10/12] Update is-integer.d.ts --- source/is-integer.d.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/is-integer.d.ts b/source/is-integer.d.ts index b5ec6c5da..56ba901fc 100644 --- a/source/is-integer.d.ts +++ b/source/is-integer.d.ts @@ -24,7 +24,8 @@ type NegativeInteger = IsInteger<-1>; type Float = IsInteger<1.5>; //=> false -// Supported non-decimal numbers +// Supports non-decimal numbers + type OctalInteger: IsInteger<0o10>; //=> true From 257348848d31963ee5c798d815f6cbd05cb3d1cf Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Mon, 22 Apr 2024 15:04:20 +0700 Subject: [PATCH 11/12] Update is-integer.ts --- test-d/is-integer.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/test-d/is-integer.ts b/test-d/is-integer.ts index 39658a1e9..968aadb13 100644 --- a/test-d/is-integer.ts +++ b/test-d/is-integer.ts @@ -15,4 +15,3 @@ expectType({} as IsInteger<0x10>); expectType({} as IsInteger<1e+100>); expectType({} as IsInteger); expectType({} as IsInteger); - From 1b8c8c1902b5d85b47d8cb5ac82e2833ffc374fb Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Mon, 22 Apr 2024 15:17:00 +0700 Subject: [PATCH 12/12] Update numeric.d.ts --- source/numeric.d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/numeric.d.ts b/source/numeric.d.ts index adfbf9e77..3edbf8db9 100644 --- a/source/numeric.d.ts +++ b/source/numeric.d.ts @@ -105,6 +105,8 @@ A `number` that is not an integer. Use-case: Validating and documenting parameters. +It does not accept `Infinity`. + @example ``` import type {Float} from 'type-fest';