Skip to content

Commit

Permalink
Refactor TypeScript definition to CommonJS compatible export (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Mar 31, 2019
1 parent aa885ea commit 0c3d690
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 20 deletions.
69 changes: 54 additions & 15 deletions index.d.ts
@@ -1,15 +1,54 @@
/**
* Make a function mimic another one. It will copy over the properties `name`, `length`, `displayName`, and any custom properties you may have set.
*
* @param to - Mimicking function.
* @param from - Function to mimic.
* @returns The modified `to` function.
*/
export default function mimicFn<
ArgumentsType extends unknown[],
ReturnType,
FunctionType extends (...arguments: ArgumentsType) => ReturnType
>(
to: (...arguments: ArgumentsType) => ReturnType,
from: FunctionType
): FunctionType;
declare const mimicFn: {
/**
Make a function mimic another one. It will copy over the properties `name`, `length`, `displayName`, and any custom properties you may have set.
@param to - Mimicking function.
@param from - Function to mimic.
@returns The modified `to` function.
@example
```
import mimicFn = require('mimic-fn');
function foo() {}
foo.unicorn = '🦄';
function wrapper() {
return foo();
}
console.log(wrapper.name);
//=> 'wrapper'
mimicFn(wrapper, foo);
console.log(wrapper.name);
//=> 'foo'
console.log(wrapper.unicorn);
//=> '🦄'
```
*/
<
ArgumentsType extends unknown[],
ReturnType,
FunctionType extends (...arguments: ArgumentsType) => ReturnType
>(
to: (...arguments: ArgumentsType) => ReturnType,
from: FunctionType
): FunctionType;

// TODO: Remove this for the next major release, refactor the whole definition to:
// declare function mimicFn<
// ArgumentsType extends unknown[],
// ReturnType,
// FunctionType extends (...arguments: ArgumentsType) => ReturnType
// >(
// to: (...arguments: ArgumentsType) => ReturnType,
// from: FunctionType
// ): FunctionType;
// export = mimicFn;
default: typeof mimicFn;
};

export = mimicFn;
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -9,4 +9,5 @@ const mimicFn = (to, from) => {
};

module.exports = mimicFn;
// TODO: Remove this for the next major release

This comment has been minimized.

Copy link
@jednano

jednano Apr 14, 2019

What is the motivation for removing default module import syntax? e.g., import mimic from 'mimic-fn'?

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Apr 15, 2019

Author Owner
module.exports.default = mimicFn;
4 changes: 2 additions & 2 deletions index.test-d.ts
@@ -1,5 +1,5 @@
import {expectType} from 'tsd-check';
import mimicFn from '.';
import {expectType} from 'tsd';
import mimicFn = require('.');

function foo(string: string) {
return false;
Expand Down
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -13,7 +13,7 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava && tsd-check"
"test": "xo && ava && tsd"
},
"files": [
"index.js",
Expand All @@ -35,8 +35,8 @@
"change"
],
"devDependencies": {
"ava": "^1.3.1",
"tsd-check": "^0.3.0",
"ava": "^1.4.1",
"tsd": "^0.7.1",
"xo": "^0.24.0"
}
}

0 comments on commit 0c3d690

Please sign in to comment.