Improve is.infinite TypeScript type
This commit is contained in:
parent
d2e65aa5b8
commit
7fbfce158e
2 changed files with 22 additions and 4 deletions
|
|
@ -2,7 +2,7 @@
|
|||
/// <reference lib="dom"/>
|
||||
/// <reference types="node"/>
|
||||
|
||||
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) => <T extends number>(value: T): value is Integer<T> => 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),
|
||||
|
|
|
|||
|
|
@ -68,3 +68,21 @@ declare function setYear<T extends number>(length: Integer<T>): 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 number> = `${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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue