index.d.ts 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. declare namespace oneTime {
  2. interface Options {
  3. /**
  4. Throw an error when called more than once.
  5. @default false
  6. */
  7. throw?: boolean;
  8. }
  9. }
  10. declare const oneTime: {
  11. /**
  12. Ensure a function is only called once. When called multiple times it will return the return value from the first call.
  13. @param fn - Function that should only be called once.
  14. @returns A function that only calls `fn` once.
  15. */
  16. <ArgumentsType extends unknown[], ReturnType>(
  17. fn: (...arguments: ArgumentsType) => ReturnType,
  18. options?: oneTime.Options
  19. ): (...arguments: ArgumentsType) => ReturnType;
  20. /**
  21. Get the number of times `fn` has been called.
  22. @param fn - Function to get call count from.
  23. @returns A number representing how many times `fn` has been called.
  24. @example
  25. ```
  26. import onetime = require('onetime');
  27. const foo = onetime(() => {});
  28. foo();
  29. foo();
  30. foo();
  31. console.log(onetime.callCount(foo));
  32. //=> 3
  33. ```
  34. */
  35. callCount(fn: (...arguments: any[]) => unknown): number;
  36. // TODO: Remove this for the next major release
  37. default: typeof oneTime;
  38. };
  39. export = oneTime;