提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

23 lines
1.2 KiB

  1. import type { AnyClass } from './isType.js';
  2. type GlobalClassName = {
  3. [K in keyof typeof globalThis]: (typeof globalThis)[K] extends AnyClass ? K : never;
  4. }[keyof typeof globalThis];
  5. /**
  6. * Checks if a value is an instance of a class or a class name. Useful when you want to check if a
  7. * value is an instance of a class that may not be defined in the current scope. For example, if you
  8. * want to check if a value is an `OffscreenCanvas` instance, you might not want to do the song and
  9. * dance of using `typeof OffscreenCanvas !== 'undefined'` and then shimming `OffscreenCanvas` if
  10. * the types aren't around.
  11. *
  12. * @example
  13. * if (isInstanceOf(value, 'OffscreenCanvas')) {
  14. * // value is an OffscreenCanvas
  15. * }
  16. *
  17. * @param value The value to recursively check
  18. * @param class_ A string or class that the value should be an instance of
  19. */
  20. export declare function isInstanceOf<T extends AnyClass>(value: unknown, class_: T): value is T;
  21. export declare function isInstanceOf<K extends GlobalClassName>(value: unknown, className: K): value is (typeof globalThis)[K];
  22. export declare function isInstanceOf(value: unknown, className: string): value is object;
  23. export {};