From 7fbfce158e196dd83a5047e869966a32b3a7fb3f Mon Sep 17 00:00:00 2001 From: Younho Choo Date: Mon, 17 Jan 2022 20:21:21 +0900 Subject: [PATCH] Improve `is.infinite` TypeScript type --- source/index.ts | 8 ++++---- source/types.ts | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/source/index.ts b/source/index.ts index cda6ac3..f459300 100644 --- a/source/index.ts +++ b/source/index.ts @@ -2,7 +2,7 @@ /// /// -import {Class, TypedArray, ObservableLike, Primitive, Integer} from './types'; +import {Class, TypedArray, ObservableLike, Primitive, Integer, PositiveInfinity, NegativeInfinity} from './types'; const typedArrayTypeNames = [ 'Int8Array', @@ -334,7 +334,7 @@ export interface NodeStream extends NodeJS.EventEmitter { is.nodeStream = (value: unknown): value is NodeStream => is.object(value) && is.function_((value as NodeStream).pipe) && !is.observable(value); -is.infinite = (value: unknown): value is number => value === Infinity || value === -Infinity; +is.infinite = (value: unknown): value is NegativeInfinity | PositiveInfinity => value === Infinity || value === -Infinity; const isAbsoluteMod2 = (remainder: number) => (value: T): value is Integer => is.integer(value) && Math.abs(value % 2) === remainder; is.evenInteger = isAbsoluteMod2(0); @@ -517,7 +517,7 @@ interface Assert { domElement: (value: unknown) => asserts value is HTMLElement; observable: (value: unknown) => asserts value is ObservableLike; nodeStream: (value: unknown) => asserts value is NodeStream; - infinite: (value: unknown) => asserts value is number; + infinite: (value: unknown) => asserts value is NegativeInfinity | PositiveInfinity; emptyArray: (value: unknown) => asserts value is never[]; nonEmptyArray: (value: unknown) => asserts value is unknown[]; emptyString: (value: unknown) => asserts value is ''; @@ -618,7 +618,7 @@ export const assert: Assert = { domElement: (value: unknown): asserts value is HTMLElement => assertType(is.domElement(value), AssertionTypeDescription.domElement, value), observable: (value: unknown): asserts value is ObservableLike => assertType(is.observable(value), 'Observable', value), nodeStream: (value: unknown): asserts value is NodeStream => assertType(is.nodeStream(value), AssertionTypeDescription.nodeStream, value), - infinite: (value: unknown): asserts value is number => assertType(is.infinite(value), AssertionTypeDescription.infinite, value), + infinite: (value: unknown): asserts value is NegativeInfinity | PositiveInfinity => assertType(is.infinite(value), AssertionTypeDescription.infinite, value), emptyArray: (value: unknown): asserts value is never[] => assertType(is.emptyArray(value), AssertionTypeDescription.emptyArray, value), nonEmptyArray: (value: unknown): asserts value is unknown[] => assertType(is.nonEmptyArray(value), AssertionTypeDescription.nonEmptyArray, value), emptyString: (value: unknown): asserts value is '' => assertType(is.emptyString(value), AssertionTypeDescription.emptyString, value), diff --git a/source/types.ts b/source/types.ts index 2b361b0..1545401 100644 --- a/source/types.ts +++ b/source/types.ts @@ -68,3 +68,21 @@ 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 = `${Type}` extends `${bigint}` ? Type : never; + +/** +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 +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 +export type NegativeInfinity = -1e999;