index.d.ts 11 KB

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