is-what
Advanced tools
Comparing version 2.4.0 to 2.5.0
@@ -36,3 +36,3 @@ 'use strict'; | ||
* @param {*} payload | ||
* @returns {payload is object} | ||
* @returns {payload is {[key: string]: any}} | ||
*/ | ||
@@ -129,11 +129,2 @@ function isObject(payload) { | ||
/** | ||
* Returns whether the payload is a Class | ||
* | ||
* @param {*} payload | ||
* @returns {payload is } | ||
*/ | ||
// export function isClass (payload: any): payload is Object { | ||
// return (getType(payload) === '') | ||
// } | ||
/** | ||
* Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String | Symbol) | ||
@@ -140,0 +131,0 @@ * |
@@ -32,3 +32,3 @@ /** | ||
* @param {*} payload | ||
* @returns {payload is object} | ||
* @returns {payload is {[key: string]: any}} | ||
*/ | ||
@@ -125,11 +125,2 @@ function isObject(payload) { | ||
/** | ||
* Returns whether the payload is a Class | ||
* | ||
* @param {*} payload | ||
* @returns {payload is } | ||
*/ | ||
// export function isClass (payload: any): payload is Object { | ||
// return (getType(payload) === '') | ||
// } | ||
/** | ||
* Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String | Symbol) | ||
@@ -136,0 +127,0 @@ * |
{ | ||
"name": "is-what", | ||
"version": "2.4.0", | ||
"version": "2.5.0", | ||
"description": "JS type check (TypeScript supported) functions like `isObject() isArray()` etc. A simple & small integration.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.cjs.js", |
@@ -38,4 +38,11 @@ # is What? 🙉 | ||
is-what makes TypeScript know the type during if statements. This means that a check returns the type of the payload for TypeScript users. | ||
```TypeScript | ||
// is-what makes TypeScript know the type during if statements: | ||
function isNumber (payload: any): payload is number { | ||
// return boolean | ||
} | ||
// As you can see above, all functions return a boolean for JavaScript, but pass the payload type to TypeScript. | ||
// usage example: | ||
function fn (payload: string | number): number { | ||
@@ -49,13 +56,23 @@ if (isNumber(payload)) { | ||
One other useful function especially for TypeScript is `isObjectLike`: | ||
`isObject` with TypeScript will declare the payload to be an object type with any props: | ||
```TypeScript | ||
function isObject (payload: any): payload is {[key: any]: any} { | ||
return isObject(payload) | ||
} | ||
// The reason to return `{[key: any]: any}` is to be able to do | ||
if (isObject(payload) && payload.id) return payload.id | ||
// if isObject() would return `payload is object` then it would give an error at `payload.id` | ||
``` | ||
If you want more control over which kind of objects are allowed you can use `isObjectLike<T>`: | ||
```TypeScript | ||
function isObjectLike<T extends object> (payload: any): payload is T { | ||
return isObject(payload) | ||
} | ||
// Eg. check if it's an object and has the `id` prop: | ||
if (isObjectLike<{id: any}>(payload)) { | ||
return payload.id | ||
} | ||
// with regular isObject() it will give an error that the prop `id` does not exist on the object. | ||
// usage examples: | ||
isObjectLike<{specificKey: string}>(payload) | ||
isObjectLike<object>(payload) | ||
// you can just pass a specific type for TS to check on. | ||
``` | ||
@@ -62,0 +79,0 @@ |
@@ -1,2 +0,1 @@ | ||
/** | ||
@@ -36,5 +35,5 @@ * Returns the object type of the given payload | ||
* @param {*} payload | ||
* @returns {payload is object} | ||
* @returns {payload is {[key: string]: any}} | ||
*/ | ||
export function isObject (payload: any): payload is object { | ||
export function isObject (payload: any): payload is {[key: string]: any} { | ||
return getType(payload) === 'Object' | ||
@@ -139,12 +138,2 @@ } | ||
/** | ||
* Returns whether the payload is a Class | ||
* | ||
* @param {*} payload | ||
* @returns {payload is } | ||
*/ | ||
// export function isClass (payload: any): payload is Object { | ||
// return (getType(payload) === '') | ||
// } | ||
/** | ||
* Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String | Symbol) | ||
@@ -151,0 +140,0 @@ * |
@@ -26,5 +26,7 @@ /** | ||
* @param {*} payload | ||
* @returns {payload is object} | ||
* @returns {payload is {[key: string]: any}} | ||
*/ | ||
export declare function isObject(payload: any): payload is object; | ||
export declare function isObject(payload: any): payload is { | ||
[key: string]: any; | ||
}; | ||
/** | ||
@@ -99,8 +101,2 @@ * Returns whether the payload is an object like a type passed in < > | ||
/** | ||
* Returns whether the payload is a Class | ||
* | ||
* @param {*} payload | ||
* @returns {payload is } | ||
*/ | ||
/** | ||
* Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String | Symbol) | ||
@@ -107,0 +103,0 @@ * |
27941
105
801