UNPKG

11.5 kBTypeScriptView Raw
1/**
2 * Returns the object type of the given payload
3 *
4 * @param {any} payload
5 * @returns {string}
6 */
7declare function getType(payload: any): string;
8
9type PlainObject = Record<string | number | symbol, any>;
10/**
11 * Returns whether the payload is a plain JavaScript object (excluding special classes or objects
12 * with other prototypes)
13 *
14 * @param {any} payload
15 * @returns {payload is PlainObject}
16 */
17declare function isPlainObject(payload: any): payload is PlainObject;
18
19/**
20 * Returns whether the payload is an any kind of object (including special classes or objects with
21 * different prototypes)
22 *
23 * @param {any} payload
24 * @returns {payload is PlainObject}
25 */
26declare function isAnyObject(payload: any): payload is PlainObject;
27
28/**
29 * Returns whether the payload is an array
30 *
31 * @param {any} payload
32 * @returns {payload is any[]}
33 */
34declare function isArray(payload: any): payload is any[];
35
36/**
37 * Returns whether the payload is a Blob
38 *
39 * @param {any} payload
40 * @returns {payload is Blob}
41 */
42declare function isBlob(payload: any): payload is Blob;
43
44/**
45 * Returns whether the payload is a boolean
46 *
47 * @param {any} payload
48 * @returns {payload is boolean}
49 */
50declare function isBoolean(payload: any): payload is boolean;
51
52/**
53 * Returns whether the payload is a Date, and that the date is valid
54 *
55 * @param {any} payload
56 * @returns {payload is Date}
57 */
58declare function isDate(payload: any): payload is Date;
59
60/**
61 * Returns whether the payload is a an empty array
62 *
63 * @param {any} payload
64 * @returns {payload is []}
65 */
66declare function isEmptyArray(payload: any): payload is [];
67
68/**
69 * Returns whether the payload is a an empty object (excluding special classes or objects with other
70 * prototypes)
71 *
72 * @param {any} payload
73 * @returns {payload is { [K in any]: never }}
74 */
75declare function isEmptyObject(payload: any): payload is {
76 [K in any]: never;
77};
78
79/**
80 * Returns whether the payload is ''
81 *
82 * @param {any} payload
83 * @returns {payload is string}
84 */
85declare function isEmptyString(payload: any): payload is string;
86
87/**
88 * Returns whether the payload is an Error
89 *
90 * @param {any} payload
91 * @returns {payload is Error}
92 */
93declare function isError(payload: any): payload is Error;
94
95/**
96 * Returns whether the payload is a File
97 *
98 * @param {any} payload
99 * @returns {payload is File}
100 */
101declare function isFile(payload: any): payload is File;
102
103/**
104 * Returns whether the payload is a an array with at least 1 item
105 *
106 * @param {any} payload
107 * @returns {payload is any[]}
108 */
109declare function isFullArray(payload: any): payload is any[];
110
111/**
112 * Returns whether the payload is a an empty object (excluding special classes or objects with other
113 * prototypes)
114 *
115 * @param {any} payload
116 * @returns {payload is PlainObject}
117 */
118declare function isFullObject(payload: any): payload is PlainObject;
119
120/**
121 * Returns whether the payload is a string, BUT returns false for ''
122 *
123 * @param {any} payload
124 * @returns {payload is string}
125 */
126declare function isFullString(payload: any): payload is string;
127
128type AnyFunction = (...args: any[]) => any;
129/**
130 * Returns whether the payload is a function (regular or async)
131 *
132 * @param {any} payload
133 * @returns {payload is AnyFunction}
134 */
135declare function isFunction(payload: any): payload is AnyFunction;
136
137type AnyClass = new (...args: any[]) => any;
138/**
139 * Does a generic check to check that the given payload is of a given type. In cases like Number, it
140 * will return true for NaN as NaN is a Number (thanks javascript!); It will, however, differentiate
141 * between object and null
142 *
143 * @template T
144 * @param {any} payload
145 * @param {T} type
146 * @returns {payload is T}
147 * @throws {TypeError} Will throw type error if type is an invalid type
148 */
149declare function isType<T extends AnyFunction | AnyClass>(payload: any, type: T): payload is T;
150
151type GlobalClassName = {
152 [K in keyof typeof globalThis]: (typeof globalThis)[K] extends AnyClass ? K : never;
153}[keyof typeof globalThis];
154/**
155 * Checks if a value is an instance of a class or a class name. Useful when you want to check if a
156 * value is an instance of a class that may not be defined in the current scope. For example, if you
157 * want to check if a value is an `OffscreenCanvas` instance, you might not want to do the song and
158 * dance of using `typeof OffscreenCanvas !== 'undefined'` and then shimming `OffscreenCanvas` if
159 * the types aren't around.
160 *
161 * @example
162 * if (isInstanceOf(value, 'OffscreenCanvas')) {
163 * // value is an OffscreenCanvas
164 * }
165 *
166 * @param value The value to recursively check
167 * @param class_ A string or class that the value should be an instance of
168 */
169declare function isInstanceOf<T extends AnyClass>(value: unknown, class_: T): value is T;
170declare function isInstanceOf<K extends GlobalClassName>(value: unknown, className: K): value is (typeof globalThis)[K];
171declare function isInstanceOf(value: unknown, className: string): value is object;
172
173/**
174 * Returns whether the payload is a Map
175 *
176 * @param {any} payload
177 * @returns {payload is Map<any, any>}
178 */
179declare function isMap(payload: any): payload is Map<any, any>;
180
181/**
182 * Returns whether the payload is literally the value `NaN` (it's `NaN` and also a `number`)
183 *
184 * @param {any} payload
185 * @returns {payload is typeof NaN}
186 */
187declare function isNaNValue(payload: any): payload is typeof NaN;
188
189/**
190 * Returns whether the payload is a negative number (but not 0)
191 *
192 * @param {any} payload
193 * @returns {payload is number}
194 */
195declare function isNegativeNumber(payload: any): payload is number;
196
197/**
198 * Returns whether the payload is null
199 *
200 * @param {any} payload
201 * @returns {payload is null}
202 */
203declare function isNull(payload: any): payload is null;
204
205/**
206 * Returns true whether the payload is null or undefined
207 *
208 * @param {any} payload
209 * @returns {(payload is null | undefined)}
210 */
211declare const isNullOrUndefined: (payload: any) => payload is null | undefined;
212
213/**
214 * Returns whether the payload is a number (but not NaN)
215 *
216 * This will return `false` for `NaN`!!
217 *
218 * @param {any} payload
219 * @returns {payload is number}
220 */
221declare function isNumber(payload: any): payload is number;
222
223/**
224 * Returns whether the payload is a plain JavaScript object (excluding special classes or objects
225 * with other prototypes)
226 *
227 * @param {any} payload
228 * @returns {payload is PlainObject}
229 */
230declare function isObject(payload: any): payload is PlainObject;
231
232/**
233 * Returns whether the payload is an object like a type passed in < >
234 *
235 * Usage: isObjectLike<{id: any}>(payload) // will make sure it's an object and has an `id` prop.
236 *
237 * @template T This must be passed in < >
238 * @param {any} payload
239 * @returns {payload is T}
240 */
241declare function isObjectLike<T extends PlainObject>(payload: any): payload is T;
242
243type TypeGuard<A, B extends A> = (payload: A) => payload is B;
244/**
245 * A factory function that creates a function to check if the payload is one of the given types.
246 *
247 * @example
248 * import { isOneOf, isNull, isUndefined } from 'is-what'
249 *
250 * const isNullOrUndefined = isOneOf(isNull, isUndefined)
251 *
252 * isNullOrUndefined(null) // true
253 * isNullOrUndefined(undefined) // true
254 * isNullOrUndefined(123) // false
255 */
256declare function isOneOf<A, B extends A, C extends A>(a: TypeGuard<A, B>, b: TypeGuard<A, C>): TypeGuard<A, B | C>;
257/**
258 * A factory function that creates a function to check if the payload is one of the given types.
259 *
260 * @example
261 * import { isOneOf, isNull, isUndefined } from 'is-what'
262 *
263 * const isNullOrUndefined = isOneOf(isNull, isUndefined)
264 *
265 * isNullOrUndefined(null) // true
266 * isNullOrUndefined(undefined) // true
267 * isNullOrUndefined(123) // false
268 */
269declare function isOneOf<A, B extends A, C extends A, D extends A>(a: TypeGuard<A, B>, b: TypeGuard<A, C>, c: TypeGuard<A, D>): TypeGuard<A, B | C | D>;
270/**
271 * A factory function that creates a function to check if the payload is one of the given types.
272 *
273 * @example
274 * import { isOneOf, isNull, isUndefined } from 'is-what'
275 *
276 * const isNullOrUndefined = isOneOf(isNull, isUndefined)
277 *
278 * isNullOrUndefined(null) // true
279 * isNullOrUndefined(undefined) // true
280 * isNullOrUndefined(123) // false
281 */
282declare function isOneOf<A, B extends A, C extends A, D extends A, E extends A>(a: TypeGuard<A, B>, b: TypeGuard<A, C>, c: TypeGuard<A, D>, d: TypeGuard<A, E>): TypeGuard<A, B | C | D | E>;
283/**
284 * A factory function that creates a function to check if the payload is one of the given types.
285 *
286 * @example
287 * import { isOneOf, isNull, isUndefined } from 'is-what'
288 *
289 * const isNullOrUndefined = isOneOf(isNull, isUndefined)
290 *
291 * isNullOrUndefined(null) // true
292 * isNullOrUndefined(undefined) // true
293 * isNullOrUndefined(123) // false
294 */
295declare function isOneOf<A, B extends A, C extends A, D extends A, E extends A, F extends A>(a: TypeGuard<A, B>, b: TypeGuard<A, C>, c: TypeGuard<A, D>, d: TypeGuard<A, E>, e: TypeGuard<A, F>): TypeGuard<A, B | C | D | E | F>;
296
297/**
298 * Returns whether the payload is a positive number (but not 0)
299 *
300 * @param {any} payload
301 * @returns {payload is number}
302 */
303declare function isPositiveNumber(payload: any): payload is number;
304
305/**
306 * Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String
307 * | Symbol)
308 *
309 * @param {any} payload
310 * @returns {(payload is boolean | null | undefined | number | string | symbol)}
311 */
312declare function isPrimitive(payload: any): payload is boolean | null | undefined | number | string | symbol;
313
314/**
315 * Returns whether the payload is a Promise
316 *
317 * @param {any} payload
318 * @returns {payload is Promise<any>}
319 */
320declare function isPromise(payload: any): payload is Promise<any>;
321
322/**
323 * Returns whether the payload is a regular expression (RegExp)
324 *
325 * @param {any} payload
326 * @returns {payload is RegExp}
327 */
328declare function isRegExp(payload: any): payload is RegExp;
329
330/**
331 * Returns whether the payload is a Set
332 *
333 * @param {any} payload
334 * @returns {payload is Set<any>}
335 */
336declare function isSet(payload: any): payload is Set<any>;
337
338/**
339 * Returns whether the payload is a string
340 *
341 * @param {any} payload
342 * @returns {payload is string}
343 */
344declare function isString(payload: any): payload is string;
345
346/**
347 * Returns whether the payload is a Symbol
348 *
349 * @param {any} payload
350 * @returns {payload is symbol}
351 */
352declare function isSymbol(payload: any): payload is symbol;
353
354/**
355 * Returns whether the payload is undefined
356 *
357 * @param {any} payload
358 * @returns {payload is undefined}
359 */
360declare function isUndefined(payload: any): payload is undefined;
361
362/**
363 * Returns whether the payload is a WeakMap
364 *
365 * @param {any} payload
366 * @returns {payload is WeakMap<any, any>}
367 */
368declare function isWeakMap(payload: any): payload is WeakMap<any, any>;
369
370/**
371 * Returns whether the payload is a WeakSet
372 *
373 * @param {any} payload
374 * @returns {payload is WeakSet<any>}
375 */
376declare function isWeakSet(payload: any): payload is WeakSet<any>;
377
378type AnyAsyncFunction = (...args: any[]) => Promise<any>;
379
380export { AnyAsyncFunction, AnyClass, AnyFunction, PlainObject, getType, isAnyObject, isArray, isBlob, isBoolean, isDate, isEmptyArray, isEmptyObject, isEmptyString, isError, isFile, isFullArray, isFullObject, isFullString, isFunction, isInstanceOf, isMap, isNaNValue, isNegativeNumber, isNull, isNullOrUndefined, isNumber, isObject, isObjectLike, isOneOf, isPlainObject, isPositiveNumber, isPrimitive, isPromise, isRegExp, isSet, isString, isSymbol, isType, isUndefined, isWeakMap, isWeakSet };