Skip to content

Commit

Permalink
fix: unknown instead of any, closes #12
Browse files Browse the repository at this point in the history
  • Loading branch information
mesqueeb committed May 24, 2024
1 parent bb6c47e commit 42cadb4
Show file tree
Hide file tree
Showing 111 changed files with 181 additions and 652 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ isInstanceOf(globalThis, ReadableStream)
is-what makes TypeScript know the type during if statements. This means that a check returns the type of the payload for TypeScript users.

```ts
function isNumber(payload: any): payload is number {
function isNumber(payload: unknown): payload is number {
// return boolean
}
// As you can see above, all functions return a boolean for JavaScript, but pass the payload type to TypeScript.
Expand All @@ -215,9 +215,9 @@ function fn(payload: string | number): number {
`isPlainObject` and `isAnyObject` with TypeScript will declare the payload to be an object type with any props:

```ts
function isPlainObject(payload: any): payload is { [key: string]: any }
function isAnyObject(payload: any): payload is { [key: string]: any }
// The reason to return `{[key: string]: any}` is to be able to do
function isPlainObject(payload: unknown): payload is { [key: string]: unknown }
function isAnyObject(payload: unknown): payload is { [key: string]: unknown }
// The reason to return `{[key: string]: unknown}` is to be able to do
if (isPlainObject(payload) && payload.id) return payload.id
// if isPlainObject() would return `payload is object` then it would give an error at `payload.id`
```
Expand All @@ -235,7 +235,7 @@ const payload = { name: 'Mesqueeb' } // current type: `{ name: string }`

// Without casting:
if (isAnyObject(payload)) {
// in here `payload` is casted to: `Record<string | number | symbol, any>`
// in here `payload` is casted to: `Record<string | number | symbol, unknown>`
// WE LOOSE THE TYPE!
}

Expand All @@ -251,7 +251,7 @@ Please note: this library will not actually check the shape of the object, you n
`isObjectLike<T>` works like this under the hood:

```ts
function isObjectLike<T extends object>(payload: any): payload is T {
function isObjectLike<T extends object>(payload: unknown): payload is T {
return isAnyObject(payload)
}
```
Expand Down
9 changes: 2 additions & 7 deletions dist/getType.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
/**
* Returns the object type of the given payload
*
* @param {any} payload
* @returns {string}
*/
export declare function getType(payload: any): string;
/** Returns the object type of the given payload */
export declare function getType(payload: unknown): string;
7 changes: 1 addition & 6 deletions dist/getType.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
/**
* Returns the object type of the given payload
*
* @param {any} payload
* @returns {string}
*/
/** Returns the object type of the given payload */
export function getType(payload) {
return Object.prototype.toString.call(payload).slice(8, -1);
}
2 changes: 1 addition & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type AnyAsyncFunction = (...args: any[]) => Promise<any>;
export type AnyAsyncFunction = (...args: unknown[]) => Promise<unknown>;
export { getType } from './getType.js';
export { isAnyObject } from './isAnyObject.js';
export { isArray } from './isArray.js';
Expand Down
5 changes: 1 addition & 4 deletions dist/isAnyObject.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@ import { PlainObject } from './isPlainObject.js';
/**
* Returns whether the payload is an any kind of object (including special classes or objects with
* different prototypes)
*
* @param {any} payload
* @returns {payload is PlainObject}
*/
export declare function isAnyObject(payload: any): payload is PlainObject;
export declare function isAnyObject(payload: unknown): payload is PlainObject;
3 changes: 0 additions & 3 deletions dist/isAnyObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import { getType } from './getType.js';
/**
* Returns whether the payload is an any kind of object (including special classes or objects with
* different prototypes)
*
* @param {any} payload
* @returns {payload is PlainObject}
*/
export function isAnyObject(payload) {
return getType(payload) === 'Object';
Expand Down
9 changes: 2 additions & 7 deletions dist/isArray.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
/**
* Returns whether the payload is an array
*
* @param {any} payload
* @returns {payload is any[]}
*/
export declare function isArray(payload: any): payload is any[];
/** Returns whether the payload is an array */
export declare function isArray(payload: unknown): payload is unknown[];
7 changes: 1 addition & 6 deletions dist/isArray.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { getType } from './getType.js';
/**
* Returns whether the payload is an array
*
* @param {any} payload
* @returns {payload is any[]}
*/
/** Returns whether the payload is an array */
export function isArray(payload) {
return getType(payload) === 'Array';
}
9 changes: 2 additions & 7 deletions dist/isBlob.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
/**
* Returns whether the payload is a Blob
*
* @param {any} payload
* @returns {payload is Blob}
*/
export declare function isBlob(payload: any): payload is Blob;
/** Returns whether the payload is a Blob */
export declare function isBlob(payload: unknown): payload is Blob;
7 changes: 1 addition & 6 deletions dist/isBlob.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { getType } from './getType.js';
/**
* Returns whether the payload is a Blob
*
* @param {any} payload
* @returns {payload is Blob}
*/
/** Returns whether the payload is a Blob */
export function isBlob(payload) {
return getType(payload) === 'Blob';
}
9 changes: 2 additions & 7 deletions dist/isBoolean.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
/**
* Returns whether the payload is a boolean
*
* @param {any} payload
* @returns {payload is boolean}
*/
export declare function isBoolean(payload: any): payload is boolean;
/** Returns whether the payload is a boolean */
export declare function isBoolean(payload: unknown): payload is boolean;
7 changes: 1 addition & 6 deletions dist/isBoolean.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { getType } from './getType.js';
/**
* Returns whether the payload is a boolean
*
* @param {any} payload
* @returns {payload is boolean}
*/
/** Returns whether the payload is a boolean */
export function isBoolean(payload) {
return getType(payload) === 'Boolean';
}
9 changes: 2 additions & 7 deletions dist/isDate.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
/**
* Returns whether the payload is a Date, and that the date is valid
*
* @param {any} payload
* @returns {payload is Date}
*/
export declare function isDate(payload: any): payload is Date;
/** Returns whether the payload is a Date, and that the date is valid */
export declare function isDate(payload: unknown): payload is Date;
7 changes: 1 addition & 6 deletions dist/isDate.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { getType } from './getType.js';
/**
* Returns whether the payload is a Date, and that the date is valid
*
* @param {any} payload
* @returns {payload is Date}
*/
/** Returns whether the payload is a Date, and that the date is valid */
export function isDate(payload) {
return getType(payload) === 'Date' && !isNaN(payload);
}
9 changes: 2 additions & 7 deletions dist/isEmptyArray.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
/**
* Returns whether the payload is a an empty array
*
* @param {any} payload
* @returns {payload is []}
*/
export declare function isEmptyArray(payload: any): payload is [];
/** Returns whether the payload is a an empty array */
export declare function isEmptyArray(payload: unknown): payload is [];
7 changes: 1 addition & 6 deletions dist/isEmptyArray.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { isArray } from './isArray.js';
/**
* Returns whether the payload is a an empty array
*
* @param {any} payload
* @returns {payload is []}
*/
/** Returns whether the payload is a an empty array */
export function isEmptyArray(payload) {
return isArray(payload) && payload.length === 0;
}
7 changes: 2 additions & 5 deletions dist/isEmptyObject.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/**
* Returns whether the payload is a an empty object (excluding special classes or objects with other
* prototypes)
*
* @param {any} payload
* @returns {payload is { [K in any]: never }}
*/
export declare function isEmptyObject(payload: any): payload is {
[K in any]: never;
export declare function isEmptyObject(payload: unknown): payload is {
[K in string | symbol | number]: never;
};
3 changes: 0 additions & 3 deletions dist/isEmptyObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import { isPlainObject } from './isPlainObject.js';
/**
* Returns whether the payload is a an empty object (excluding special classes or objects with other
* prototypes)
*
* @param {any} payload
* @returns {payload is { [K in any]: never }}
*/
export function isEmptyObject(payload) {
return isPlainObject(payload) && Object.keys(payload).length === 0;
Expand Down
9 changes: 2 additions & 7 deletions dist/isEmptyString.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
/**
* Returns whether the payload is ''
*
* @param {any} payload
* @returns {payload is string}
*/
export declare function isEmptyString(payload: any): payload is string;
/** Returns whether the payload is '' */
export declare function isEmptyString(payload: unknown): payload is string;
7 changes: 1 addition & 6 deletions dist/isEmptyString.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
/**
* Returns whether the payload is ''
*
* @param {any} payload
* @returns {payload is string}
*/
/** Returns whether the payload is '' */
export function isEmptyString(payload) {
return payload === '';
}
9 changes: 2 additions & 7 deletions dist/isError.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
/**
* Returns whether the payload is an Error
*
* @param {any} payload
* @returns {payload is Error}
*/
export declare function isError(payload: any): payload is Error;
/** Returns whether the payload is an Error */
export declare function isError(payload: unknown): payload is Error;
7 changes: 1 addition & 6 deletions dist/isError.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { getType } from './getType.js';
/**
* Returns whether the payload is an Error
*
* @param {any} payload
* @returns {payload is Error}
*/
/** Returns whether the payload is an Error */
export function isError(payload) {
return getType(payload) === 'Error' || payload instanceof Error;
}
9 changes: 2 additions & 7 deletions dist/isFile.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
/**
* Returns whether the payload is a File
*
* @param {any} payload
* @returns {payload is File}
*/
export declare function isFile(payload: any): payload is File;
/** Returns whether the payload is a File */
export declare function isFile(payload: unknown): payload is File;
7 changes: 1 addition & 6 deletions dist/isFile.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { getType } from './getType.js';
/**
* Returns whether the payload is a File
*
* @param {any} payload
* @returns {payload is File}
*/
/** Returns whether the payload is a File */
export function isFile(payload) {
return getType(payload) === 'File';
}
9 changes: 2 additions & 7 deletions dist/isFullArray.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
/**
* Returns whether the payload is a an array with at least 1 item
*
* @param {any} payload
* @returns {payload is any[]}
*/
export declare function isFullArray(payload: any): payload is any[];
/** Returns whether the payload is a an array with at least 1 item */
export declare function isFullArray(payload: unknown): payload is unknown[];
7 changes: 1 addition & 6 deletions dist/isFullArray.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { isArray } from './isArray.js';
/**
* Returns whether the payload is a an array with at least 1 item
*
* @param {any} payload
* @returns {payload is any[]}
*/
/** Returns whether the payload is a an array with at least 1 item */
export function isFullArray(payload) {
return isArray(payload) && payload.length > 0;
}
5 changes: 1 addition & 4 deletions dist/isFullObject.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@ import { PlainObject } from './isPlainObject.js';
/**
* Returns whether the payload is a an empty object (excluding special classes or objects with other
* prototypes)
*
* @param {any} payload
* @returns {payload is PlainObject}
*/
export declare function isFullObject(payload: any): payload is PlainObject;
export declare function isFullObject(payload: unknown): payload is PlainObject;
3 changes: 0 additions & 3 deletions dist/isFullObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import { isPlainObject } from './isPlainObject.js';
/**
* Returns whether the payload is a an empty object (excluding special classes or objects with other
* prototypes)
*
* @param {any} payload
* @returns {payload is PlainObject}
*/
export function isFullObject(payload) {
return isPlainObject(payload) && Object.keys(payload).length > 0;
Expand Down
9 changes: 2 additions & 7 deletions dist/isFullString.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
/**
* Returns whether the payload is a string, BUT returns false for ''
*
* @param {any} payload
* @returns {payload is string}
*/
export declare function isFullString(payload: any): payload is string;
/** Returns whether the payload is a string, BUT returns false for '' */
export declare function isFullString(payload: unknown): payload is string;
7 changes: 1 addition & 6 deletions dist/isFullString.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { isString } from './isString.js';
/**
* Returns whether the payload is a string, BUT returns false for ''
*
* @param {any} payload
* @returns {payload is string}
*/
/** Returns whether the payload is a string, BUT returns false for '' */
export function isFullString(payload) {
return isString(payload) && payload !== '';
}
11 changes: 3 additions & 8 deletions dist/isFunction.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
export type AnyFunction = (...args: any[]) => any;
/**
* Returns whether the payload is a function (regular or async)
*
* @param {any} payload
* @returns {payload is AnyFunction}
*/
export declare function isFunction(payload: any): payload is AnyFunction;
export type AnyFunction = (...args: unknown[]) => unknown;
/** Returns whether the payload is a function (regular or async) */
export declare function isFunction(payload: unknown): payload is AnyFunction;
7 changes: 1 addition & 6 deletions dist/isFunction.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
/**
* Returns whether the payload is a function (regular or async)
*
* @param {any} payload
* @returns {payload is AnyFunction}
*/
/** Returns whether the payload is a function (regular or async) */
export function isFunction(payload) {
return typeof payload === 'function';
}
9 changes: 2 additions & 7 deletions dist/isMap.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
/**
* Returns whether the payload is a Map
*
* @param {any} payload
* @returns {payload is Map<any, any>}
*/
export declare function isMap(payload: any): payload is Map<any, any>;
/** Returns whether the payload is a Map */
export declare function isMap(payload: unknown): payload is Map<unknown, unknown>;
7 changes: 1 addition & 6 deletions dist/isMap.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { getType } from './getType.js';
/**
* Returns whether the payload is a Map
*
* @param {any} payload
* @returns {payload is Map<any, any>}
*/
/** Returns whether the payload is a Map */
export function isMap(payload) {
return getType(payload) === 'Map';
}
Loading

0 comments on commit 42cadb4

Please sign in to comment.