Skip to content

Commit

Permalink
fix: replace use of type guards with boolean return type
Browse files Browse the repository at this point in the history
The type narrowing behavior of type guards makes only sense when the
runtime checks match the type guards. For example `value is string`
should be used with `typeof value === 'string'` and not with a function
that checks for whether a value is a snakecase string. In the latter
case, the type guard would incorrectly narrow the type of the value to
`never` for a string that is not snakecase.
  • Loading branch information
Planeshifter committed Feb 28, 2024
1 parent 670aeab commit 30112b7
Show file tree
Hide file tree
Showing 33 changed files with 41 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
* var bool = isAbsoluteHttpURI( null );
* // returns false
*/
declare function isAbsoluteHttpURI( value: any ): value is string;
declare function isAbsoluteHttpURI( value: any ): boolean;

Check warning on line 51 in lib/node_modules/@stdlib/assert/is-absolute-http-uri/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ interface IsAbsolutePath {
* // returns true
* }
*/
( value: any ): value is string;
( value: any ): boolean;

Check warning on line 42 in lib/node_modules/@stdlib/assert/is-absolute-path/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Tests if a value is a POSIX absolute path.
Expand All @@ -55,7 +55,7 @@ interface IsAbsolutePath {
* var bool = isAbsolutePath.posix( 'foo/bar/baz' );
* // returns false
*/
posix( value: any ): value is string;
posix( value: any ): boolean;

Check warning on line 58 in lib/node_modules/@stdlib/assert/is-absolute-path/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Tests if a value is a Windows absolute path.
Expand All @@ -71,7 +71,7 @@ interface IsAbsolutePath {
* var bool = isAbsolutePath.win32( 'foo\\bar\\baz' );
* // returns false
*/
win32( value: any ): value is string;
win32( value: any ): boolean;

Check warning on line 74 in lib/node_modules/@stdlib/assert/is-absolute-path/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type
}

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

Check warning on line 47 in lib/node_modules/@stdlib/assert/is-absolute-uri/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
* var out = isAlphagram( 123 );
* // returns false
*/
declare function isAlphagram( value: any ): value is string;
declare function isAlphagram( value: any ): boolean;

Check warning on line 51 in lib/node_modules/@stdlib/assert/is-alphagram/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
* var out = isAlphaNumeric( 123 );
* // returns false
*/
declare function isAlphaNumeric( value: any ): value is string;
declare function isAlphaNumeric( value: any ): boolean;

Check warning on line 47 in lib/node_modules/@stdlib/assert/is-alphanumeric/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* var out = isASCII( 123 );
* // returns false
*/
declare function isASCII( value: any ): value is string;
declare function isASCII( value: any ): boolean;

Check warning on line 43 in lib/node_modules/@stdlib/assert/is-ascii/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type


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

Check warning on line 39 in lib/node_modules/@stdlib/assert/is-binary-string/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
* var bool = isBlankString( 'beep' );
* // returns false
*/
declare function isBlankString( value: any ): value is string;
declare function isBlankString( value: any ): boolean;

Check warning on line 47 in lib/node_modules/@stdlib/assert/is-blank-string/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* var bool = isCamelcase( 'hello world' );
* // returns false
*/
declare function isCamelcase( value: any ): value is string;
declare function isCamelcase( value: any ): boolean;


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* var bool = isCapitalized( 'salt and light' );
* // returns false
*/
declare function isCapitalized( value: any ): value is string;
declare function isCapitalized( value: any ): boolean;


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


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* var out = isDigitString( 123 );
* // returns false
*/
declare function isDigitString( value: any ): value is string;
declare function isDigitString( value: any ): boolean;


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* var bool = isDomainName( 'foo@bar.com' );
* // returns false
*/
declare function isDomainName( value: any ): value is string;
declare function isDomainName( value: any ): boolean;


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
* var bool = isDurationString( '1d2h' );
* // returns true
*/
declare function isDurationString( value: any ): value is string;
declare function isDurationString( value: any ): boolean;


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


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ interface IsEmptyString {
* var bool = isEmptyString( [] );
* // returns false
*/
( value: any ): value is string | String;
( value: any ): boolean;

/**
* Tests if a value is an empty string primitive.
Expand All @@ -60,7 +60,7 @@ interface IsEmptyString {
* var bool = isEmptyString.isPrimitive( [] );
* // returns false
*/
isPrimitive( value: any ): value is string;
isPrimitive( value: any ): boolean;

/**
* Tests if a value is an empty string object.
Expand All @@ -80,7 +80,7 @@ interface IsEmptyString {
* var bool = isEmptyString.isObject( [] );
* // returns false
*/
isObject( value: any ): value is String;
isObject( value: any ): boolean;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* var out = isHexString( 123 );
* // returns false
*/
declare function isHexString( value: any ): value is string;
declare function isHexString( value: any ): boolean;


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* var v = isJSON( '{a":5}' );
* // returns false
*/
declare function isJSON( value: any ): value is string;
declare function isJSON( value: any ): boolean;


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


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


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* var bool = isLowercase( '!' );
* // returns false
*/
declare function isLowercase( value: any ): value is string;
declare function isLowercase( value: any ): boolean;


// EXPORTS //
Expand Down
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 ): value is string;
declare function isPascalcase( value: any ): boolean;


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


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ interface IsRelativePath {
* // returns false
* }
*/
( value: any ): value is string;
( value: any ): boolean;

/**
* Tests if a value is a POSIX relative path.
Expand All @@ -61,7 +61,7 @@ interface IsRelativePath {
* var bool = isRelativePath.posix( '/foo/../bar/baz' );
* // returns false
*/
posix( value: any ): value is string;
posix( value: any ): boolean;

/**
* Tests if a value is a Windows relative path.
Expand All @@ -77,7 +77,7 @@ interface IsRelativePath {
* var bool = isRelativePath.win32( 'C:\\foo\\..\\bar\\baz' );
* // returns false
*/
win32( value: any ): value is string;
win32( value: any ): boolean;
}

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


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


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


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* bool = isStartcase( 'Beep and Boop' );
* // returns false
*/
declare function isStartcase( value: any ): value is string;
declare function isStartcase( value: any ): boolean;


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* var bool = isUNCPath( '/foo/bar/baz' );
* // returns false
*/
declare function isUNCPath( value: any ): value is string;
declare function isUNCPath( value: any ): boolean;


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* var bool = isUppercase( 'salt and light' );
* // returns false
*/
declare function isUppercase( value: any ): value is string;
declare function isUppercase( value: any ): boolean;


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
* var bool = isURI( 'http://example.w3.org/%at' );
* // returns false
*/
declare function isURI( value: any ): value is string;
declare function isURI( value: any ): boolean;


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ interface isWellFormedString {
* var bool = isWellFormedString( null );
* // returns false
*/
( str: any ): str is string | String;
( str: any ): boolean;

/**
* Tests if a string is a well-formed string primitive.
Expand All @@ -72,7 +72,7 @@ interface isWellFormedString {
* var bool = isWellFormedString.isPrimitive( new String( '' ) );
* // returns false
*/
isPrimitive( str: any ): str is string;
isPrimitive( str: any ): boolean;

/**
* Tests if a string is a well-formed string object.
Expand All @@ -88,7 +88,7 @@ interface isWellFormedString {
* var bool = isWellFormedString.isObject( new String( '' ) );
* // returns true
*/
isObject( str: any ): str is Object;
isObject( str: any ): boolean;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* var out = isWhitespace( 123 );
* // returns false
*/
declare function isWhitespace( x: any ): x is string;
declare function isWhitespace( x: any ): boolean;


// EXPORTS //
Expand Down

1 comment on commit 30112b7

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
array/base/count-same-value-zero $\color{red}210/212$
$\color{green}+99.06\%$
$\color{red}15/16$
$\color{green}+93.75\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{red}210/212$
$\color{green}+99.06\%$
assert/is-absolute-http-uri $\color{green}113/113$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}113/113$
$\color{green}+100.00\%$
assert/is-absolute-path $\color{green}195/195$
$\color{green}+100.00\%$
$\color{green}24/24$
$\color{green}+100.00\%$
$\color{green}2/2$
$\color{green}+100.00\%$
$\color{green}195/195$
$\color{green}+100.00\%$
assert/is-absolute-uri $\color{green}104/104$
$\color{green}+100.00\%$
$\color{green}3/3$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}104/104$
$\color{green}+100.00\%$
assert/is-alphagram $\color{green}122/122$
$\color{green}+100.00\%$
$\color{green}10/10$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}122/122$
$\color{green}+100.00\%$
assert/is-alphanumeric $\color{green}138/138$
$\color{green}+100.00\%$
$\color{green}15/15$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}138/138$
$\color{green}+100.00\%$
assert/is-ascii $\color{green}122/122$
$\color{green}+100.00\%$
$\color{green}10/10$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}122/122$
$\color{green}+100.00\%$
assert/is-binary-string $\color{green}113/113$
$\color{green}+100.00\%$
$\color{green}11/11$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}113/113$
$\color{green}+100.00\%$
assert/is-blank-string $\color{green}116/116$
$\color{green}+100.00\%$
$\color{green}5/5$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}116/116$
$\color{green}+100.00\%$
assert/is-camelcase $\color{green}104/104$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}104/104$
$\color{green}+100.00\%$
assert/is-capitalized $\color{green}107/107$
$\color{green}+100.00\%$
$\color{green}7/7$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}107/107$
$\color{green}+100.00\%$
assert/is-constantcase $\color{green}110/110$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}110/110$
$\color{green}+100.00\%$
assert/is-digit-string $\color{green}126/126$
$\color{green}+100.00\%$
$\color{green}10/10$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}126/126$
$\color{green}+100.00\%$
assert/is-domain-name $\color{green}157/157$
$\color{green}+100.00\%$
$\color{green}8/8$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}157/157$
$\color{green}+100.00\%$
assert/is-duration-string $\color{green}119/119$
$\color{green}+100.00\%$
$\color{green}5/5$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}119/119$
$\color{green}+100.00\%$
assert/is-email-address $\color{green}108/108$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}108/108$
$\color{green}+100.00\%$
assert/is-empty-string $\color{green}231/231$
$\color{green}+100.00\%$
$\color{green}9/9$
$\color{green}+100.00\%$
$\color{green}3/3$
$\color{green}+100.00\%$
$\color{green}231/231$
$\color{green}+100.00\%$
assert/is-hex-string $\color{green}135/135$
$\color{green}+100.00\%$
$\color{green}13/13$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}135/135$
$\color{green}+100.00\%$
assert/is-json $\color{green}149/149$
$\color{green}+100.00\%$
$\color{green}9/9$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}149/149$
$\color{green}+100.00\%$
assert/is-kebabcase $\color{green}101/101$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}101/101$
$\color{green}+100.00\%$
assert/is-localhost $\color{green}130/130$
$\color{green}+100.00\%$
$\color{green}8/8$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}130/130$
$\color{green}+100.00\%$
assert/is-lowercase $\color{green}106/106$
$\color{green}+100.00\%$
$\color{green}5/5$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}106/106$
$\color{green}+100.00\%$
assert/is-pascalcase $\color{green}101/101$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}101/101$
$\color{green}+100.00\%$
assert/is-regexp-string $\color{green}111/111$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}111/111$
$\color{green}+100.00\%$
assert/is-relative-path $\color{green}174/174$
$\color{green}+100.00\%$
$\color{green}9/9$
$\color{green}+100.00\%$
$\color{green}2/2$
$\color{green}+100.00\%$
$\color{green}174/174$
$\color{green}+100.00\%$
assert/is-relative-uri $\color{red}202/205$
$\color{green}+98.54\%$
$\color{red}16/18$
$\color{green}+88.89\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{red}202/205$
$\color{green}+98.54\%$
assert/is-semver $\color{green}101/101$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}101/101$
$\color{green}+100.00\%$
assert/is-snakecase $\color{green}105/105$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}105/105$
$\color{green}+100.00\%$
assert/is-startcase $\color{green}95/95$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}95/95$
$\color{green}+100.00\%$
assert/is-unc-path $\color{green}96/96$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}96/96$
$\color{green}+100.00\%$
assert/is-uppercase $\color{green}106/106$
$\color{green}+100.00\%$
$\color{green}5/5$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}106/106$
$\color{green}+100.00\%$
assert/is-uri $\color{green}354/354$
$\color{green}+100.00\%$
$\color{green}20/20$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}354/354$
$\color{green}+100.00\%$
assert/is-well-formed-string $\color{red}334/340$
$\color{green}+98.24\%$
$\color{red}25/27$
$\color{green}+92.59\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{red}334/340$
$\color{green}+98.24\%$
assert/is-whitespace $\color{green}112/112$
$\color{green}+100.00\%$
$\color{green}5/5$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}112/112$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.