Skip to content

Commit

Permalink
fix: Replace any arg types in type guards w/ unknown
Browse files Browse the repository at this point in the history
  • Loading branch information
crgwbr committed Jul 23, 2024
1 parent dc99c6f commit 4166c13
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export * from './utils/has-defined';
export * from './utils/is-arguments';
export * from './utils/is-array';
export * from './utils/is-array-of-strings';
export * from './utils/is-boolean';
export * from './utils/is-empty';
export * from './utils/is-enum-value';
export * from './utils/is-map-with-values-of-type';
Expand Down
2 changes: 1 addition & 1 deletion src/utils/is-arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import { getTagString } from './get-tag-string';
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
* @returns `true` if `o` is a function's array-like `arguments` variable
*/
export function isArguments(o: any): boolean {
export function isArguments(o: unknown): o is IArguments {
return getTagString(o) === '[object Arguments]';
}
2 changes: 1 addition & 1 deletion src/utils/is-array-of-strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { isString } from './is-string';
* not counted as `string` values, even if the TypeScript compiler's `strictNullChecks`
* flag is set to `false` in your project.
*/
export function isArrayOfStrings(values: any): values is string[] {
export function isArrayOfStrings(values: unknown): values is string[] {
if (!isArray(values)) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/is-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
*
* @returns `true` if `o` is an `Array`, regardless of the types that it contains
*/
export function isArray(o: any): o is unknown[] {
export function isArray(o: unknown): o is unknown[] {
return Array.isArray(o);
}
3 changes: 3 additions & 0 deletions src/utils/is-boolean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isBoolean(o: unknown): o is boolean {
return o === true || o === false;
}
10 changes: 7 additions & 3 deletions src/utils/is-empty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { isArray } from './is-array';
import { isString } from './is-string';
import { isArguments } from './is-arguments';
import { isUndefined } from './is-undefined';
import { isNull } from './is-null';
import { isBoolean } from './is-boolean';
import { isNumber } from './is-number';
import { isObject } from './is-object';

/**
* Checks if `o` is an empty object. An object is "empty" if it:
Expand All @@ -12,12 +16,12 @@ import { isUndefined } from './is-undefined';
*
* @returns `true` if `o` is empty
*/
export function isEmpty(o: any): boolean {
if (o === null || isUndefined(o)) {
export function isEmpty(o: unknown): boolean {
if (isNull(o) || isUndefined(o) || isBoolean(o) || isNumber(o)) {
return true;
}
if (isArray(o) || isString(o) || isArguments(o)) {
return o.length === 0;
}
return Object.keys(o).length === 0;
return isObject(o) && Object.keys(o).length === 0;
}
2 changes: 1 addition & 1 deletion src/utils/is-enum-value.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Type guard to check to see if the given value is a valid value for the enum.
*/
export function isEnumValue<T>(enumType: T, value: any): value is T[keyof T] {
export function isEnumValue<T>(enumType: T, value: unknown): value is T[keyof T] {
return (Object.keys(enumType) as Array<keyof T>)
.map((key) => {
return enumType[key];
Expand Down
2 changes: 1 addition & 1 deletion src/utils/is-number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import { isObject } from './is-object';
* considered `number`s. If you need to check for one of those values, you can use the
* built-in `Number.isNaN` or `Number.isFinite` functions.
*/
export function isNumber(o: any): o is number {
export function isNumber(o: unknown): o is number {
return typeof o === 'number' || (isObject(o) && getTagString(o) === '[object Number]');
}
2 changes: 1 addition & 1 deletion src/utils/is-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @see https://github.com/jashkenas/underscore/blob/d5fe0fd4060f13b40608cb9d92eda6d857e8752c/underscore.js#L1322
* @returns `true` if `o` is an `object`
*/
export function isObject(o: any): o is object {
export function isObject(o: unknown): o is object {
let type = typeof o;

return o !== null && (type === 'object' || type === 'function');
Expand Down
2 changes: 1 addition & 1 deletion src/utils/is-promise-like.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import { isObject } from './is-object';
*
* @returns `true` if `o` is `Promise`-like (i.e. has a `then` function)
*/
export function isPromiseLike(o: any): o is PromiseLike<unknown> {
export function isPromiseLike(o: unknown): o is PromiseLike<unknown> {
return isPromise(o) || (isObject(o) && typeof (o as any).then === 'function');
}
2 changes: 1 addition & 1 deletion src/utils/is-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import { getTagString } from './get-tag-string';
*
* @returns `true` if `o` is a `Promise`
*/
export function isPromise(o: any): o is Promise<unknown> {
export function isPromise(o: unknown): o is Promise<unknown> {
return isObject(o) && getTagString(o) === '[object Promise]';
}
2 changes: 1 addition & 1 deletion src/utils/is-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import { getTagString } from './get-tag-string';
* `string` values, even if the TypeScript compiler's `strictNullChecks` flag is set to
* `false` in your project.
*/
export function isString(o: any): o is string {
export function isString(o: unknown): o is string {
return typeof o === 'string' || (isObject(o) && getTagString(o) === '[object String]');
}
16 changes: 16 additions & 0 deletions tests/utils/is-boolean.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expect } from 'chai';
import * as t from '../../src/index';


describe('isBoolean', () => {

it('correctly classifies bools', () => {
expect(t.isBoolean(true)).to.strictlyEqual(true);
expect(t.isBoolean(false)).to.strictlyEqual(true);
expect(t.isBoolean(null)).to.strictlyEqual(false);
expect(t.isBoolean(0)).to.strictlyEqual(false);
expect(t.isBoolean('')).to.strictlyEqual(false);
expect(t.isBoolean({})).to.strictlyEqual(false);
});

});

0 comments on commit 4166c13

Please sign in to comment.