index.d.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. declare namespace mimicFn {
  2. interface Options {
  3. /**
  4. Skip modifying [non-configurable properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor#Description) instead of throwing an error.
  5. @default false
  6. */
  7. readonly ignoreNonConfigurable?: boolean;
  8. }
  9. }
  10. /**
  11. Modifies the `to` function to mimic the `from` function. Returns the `to` function.
  12. `name`, `displayName`, and any other properties of `from` are copied. The `length` property is not copied. Prototype, class, and inherited properties are copied.
  13. `to.toString()` will return the same as `from.toString()` but prepended with a `Wrapped with to()` comment.
  14. @param to - Mimicking function.
  15. @param from - Function to mimic.
  16. @returns The modified `to` function.
  17. @example
  18. ```
  19. import mimicFn = require('mimic-fn');
  20. function foo() {}
  21. foo.unicorn = '🦄';
  22. function wrapper() {
  23. return foo();
  24. }
  25. console.log(wrapper.name);
  26. //=> 'wrapper'
  27. mimicFn(wrapper, foo);
  28. console.log(wrapper.name);
  29. //=> 'foo'
  30. console.log(wrapper.unicorn);
  31. //=> '🦄'
  32. ```
  33. */
  34. declare function mimicFn<
  35. ArgumentsType extends unknown[],
  36. ReturnType,
  37. FunctionType extends (...arguments: ArgumentsType) => ReturnType
  38. >(
  39. to: (...arguments: ArgumentsType) => ReturnType,
  40. from: FunctionType,
  41. options?: mimicFn.Options,
  42. ): FunctionType;
  43. export = mimicFn;