Skip to content

Commit

Permalink
feat: use type predicates for narrowing
Browse files Browse the repository at this point in the history
  • Loading branch information
Planeshifter committed Jun 28, 2023
1 parent 757db5e commit 196225d
Show file tree
Hide file tree
Showing 14 changed files with 30 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* var bool = isPascalcase( null );
* // returns false
*/
declare function isPascalcase( value: any ): boolean;
declare function isPascalcase( value: any ): value is string;


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* bool = isPlainObjectArray( [ {}, '3.0' ] );
* // returns false
*/
declare function isPlainObjectArray( value: any ): boolean;
declare function isPlainObjectArray( value: any ): value is ArrayLike<object>;


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* var bool = isPlainObject( null );
* // returns false
*/
declare function isPlainObject( value: any ): boolean;
declare function isPlainObject( value: any ): value is object;


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ interface IsPositiveIntegerArray {
* var bool = isPositiveIntegerArray( [ 3.0, '3.0' ] );
* // returns false
*/
( value: any ): boolean;
( value: any ): value is ArrayLike<number | Number>;

/**
* Tests if a value is an array-like object containing only positive primitive integer values.
Expand All @@ -52,7 +52,7 @@ interface IsPositiveIntegerArray {
* var bool = isPositiveIntegerArray.primitives( [ 3.0, new Number(1.0) ] );
* // returns false
*/
primitives( value: any ): boolean;
primitives( value: any ): value is ArrayLike<number>;

/**
* Tests if a value is an array-like object containing only number objects having positive integer values.
Expand All @@ -68,7 +68,7 @@ interface IsPositiveIntegerArray {
* var bool = isPositiveIntegerArray.objects( [ 1.0, 2.0, 10.0 ] );
* // returns false
*/
objects( value: any ): boolean;
objects( value: any ): value is ArrayLike<Number>;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ interface IsPositiveInteger {
* var bool = isPositiveInteger( null );
* // returns false
*/
( value: any ): boolean;
( value: any ): value is number | Number;

/**
* Tests if a value is a number primitive having a positive integer value.
Expand All @@ -68,7 +68,7 @@ interface IsPositiveInteger {
* var bool = isPositiveInteger.isPrimitive( new Number( 3.0 ) );
* // returns false
*/
isPrimitive( value: any ): boolean;
isPrimitive( value: any ): value is number;

/**
* Tests if a value is a number object having a positive integer value.
Expand All @@ -84,7 +84,7 @@ interface IsPositiveInteger {
* var bool = isPositiveInteger.isObject( new Number( 3.0 ) );
* // returns true
*/
isObject( value: any ): boolean;
isObject( value: any ): value is Number;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ interface IsPositiveNumberArray {
* var bool = isPositiveNumberArray( [ 3.0, '3.0' ] );
* // returns false
*/
( value: any ): boolean;
( value: any ): value is ArrayLike<number | Number>;

/**
* Tests if a value is an array-like object containing only positive primitive number values.
Expand All @@ -52,7 +52,7 @@ interface IsPositiveNumberArray {
* var bool = isPositiveNumberArray.primitives( [ 3.0, new Number(1.0) ] );
* // returns false
*/
primitives( value: any ): boolean;
primitives( value: any ): value is ArrayLike<number>;

/**
* Tests if a value is an array-like object containing only number objects having positive number values.
Expand All @@ -68,7 +68,7 @@ interface IsPositiveNumberArray {
* var bool = isPositiveNumberArray.objects( [ 1.0, 2.7, 10.0 ] );
* // returns false
*/
objects( value: any ): boolean;
objects( value: any ): value is ArrayLike<Number>;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ interface IsPositiveNumber {
* var bool = isPositiveNumber( null );
* // returns false
*/
( value: any ): boolean;
( value: any ): value is number | Number;

/**
* Tests if a value is a number primitive having a positive value.
Expand All @@ -64,7 +64,7 @@ interface IsPositiveNumber {
* var bool = isPositiveNumber.isPrimitive( new Number( 3.0 ) );
* // returns false
*/
isPrimitive( value: any ): boolean;
isPrimitive( value: any ): value is number;

/**
* Tests if a value is a number object having a positive value.
Expand All @@ -80,7 +80,7 @@ interface IsPositiveNumber {
* var bool = isPositiveNumber.isObject( new Number( 3.0 ) );
* // returns true
*/
isObject( value: any ): boolean;
isObject( value: any ): value is Number;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ interface IsPositiveZero {
* var bool = isPositiveZero( null );
* // returns false
*/
( value: any ): boolean;
( value: any ): value is number | Number;

/**
* Tests if a value is a number primitive equal to positive zero.
Expand All @@ -68,7 +68,7 @@ interface IsPositiveZero {
* var bool = isPositiveZero.isPrimitive( new Number( 0.0 ) );
* // returns false
*/
isPrimitive( value: any ): boolean;
isPrimitive( value: any ): value is number;

/**
* Tests if a value is a number object having a value equal to positive zero.
Expand All @@ -84,7 +84,7 @@ interface IsPositiveZero {
* var bool = isPositiveZero.isObject( new Number( 0.0 ) );
* // returns true
*/
isObject( value: any ): boolean;
isObject( value: any ): value is Number;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ interface IsPrime {
* var bool = isPrime( null );
* // returns false
*/
( value: any ): boolean;
( value: any ): value is number | Number;

/**
* Tests if a value is a number primitive having a value which is a prime number.
Expand All @@ -64,7 +64,7 @@ interface IsPrime {
* var bool = isPrime.isPrimitive( new Number( 5.0 ) );
* // returns false
*/
isPrimitive( value: any ): boolean;
isPrimitive( value: any ): value is number;

/**
* Tests if a value is a number object having a value which is a prime number.
Expand All @@ -80,7 +80,7 @@ interface IsPrime {
* var bool = isPrime.isObject( new Number( 5.0 ) );
* // returns true
*/
isObject( value: any ): boolean;
isObject( value: any ): value is Number;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* bool = isPrimitiveArray( [ new String('abc'), '3.0' ] );
* // returns false
*/
declare function isPrimitiveArray( value: any ): boolean;
declare function isPrimitiveArray( value: any ): value is Array<any>;

This comment has been minimized.

Copy link
@kgryte

kgryte Jun 28, 2023

Member

Would it be worth being more specific here as done for is-primitive?



// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* var bool = isPrimitive( {} );
* // returns false
*/
declare function isPrimitive( value: any ): boolean;
declare function isPrimitive( value: any ): value is null | undefined | string | boolean | number | symbol;


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ interface IsProbabilityArray {
* var bool = isProbabilityArray( [ 0.3, '0.3' ] );
* // returns false
*/
( value: any ): boolean;
( value: any ): value is ArrayLike<number | Number>;

/**
* Tests if a value is an array-like object containing only primitive probabilities.
Expand All @@ -52,7 +52,7 @@ interface IsProbabilityArray {
* var bool = isProbabilityArray.primitives( [ 0.3, new Number(0.1) ] );
* // returns false
*/
primitives( value: any ): boolean;
primitives( value: any ): value is ArrayLike<number>;

/**
* Tests if a value is an array-like object containing only number objects having probability values.
Expand All @@ -68,7 +68,7 @@ interface IsProbabilityArray {
* var bool = isProbabilityArray.objects( [ 0.1, 0.2, 0.3 ] );
* // returns false
*/
objects( value: any ): boolean;
objects( value: any ): value is ArrayLike<Number>;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ interface IsProbability {
* var bool = isProbability( null );
* // returns false
*/
( value: any ): boolean;
( value: any ): value is number | Number;

/**
* Tests if a value is a number primitive having a value which is a probability.
Expand All @@ -64,7 +64,7 @@ interface IsProbability {
* var bool = isProbability.isPrimitive( new Number( 0.66 ) );
* // returns false
*/
isPrimitive( value: any ): boolean;
isPrimitive( value: any ): value is number;

/**
* Tests if a value is a number object having a value which is a probability.
Expand All @@ -80,7 +80,7 @@ interface IsProbability {
* var bool = isProbability.isObject( new Number( 0.5 ) );
* // returns true
*/
isObject( value: any ): boolean;
isObject( value: any ): value is Number;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
* var bool = isPropertyKey( [] );
* // returns false
*/
declare function isPropertyKey( value: any ): boolean;
declare function isPropertyKey( value: any ): value is PropertyKey;


// EXPORTS //
Expand Down

0 comments on commit 196225d

Please sign in to comment.