diff --git a/packages/function-demethodize/index.d.ts b/packages/function-demethodize/index.d.ts new file mode 100644 index 000000000..0ab8a62d3 --- /dev/null +++ b/packages/function-demethodize/index.d.ts @@ -0,0 +1,13 @@ +declare function demethodize( + method: Entity[MethodName] & Function +): Entity[MethodName] extends (...p: any[]) => any + ? ( + p: Entity, + ...args: Parameters + ) => ReturnType + : never; +declare function demethodize( + method: (...p: Params) => Returns +): (p: Entity, ...a: Params) => Returns; + +export default demethodize; diff --git a/packages/function-demethodize/index.tests.ts b/packages/function-demethodize/index.tests.ts new file mode 100644 index 000000000..b1835e476 --- /dev/null +++ b/packages/function-demethodize/index.tests.ts @@ -0,0 +1,22 @@ +import demethodize from "./index"; + +//OK + +const test1: (p: string) => string = demethodize("".trim); +const test2 = demethodize("".big); +const test3 = demethodize((1).toFixed); +const test4 = demethodize({}.toString); +const test5 = demethodize([].map); +const test6 = demethodize("".split); + +// Not OK +// @ts-expect-error +demethodize(); +// @ts-expect-error +demethodize((1).toExponential); +// @ts-expect-error +demethodize([].toString); +// @ts-expect-error +demethodize([].length); +// @ts-expect-error +demethodize("".trim); diff --git a/packages/function-demethodize/package.json b/packages/function-demethodize/package.json index 4bbf551ef..8febe20ba 100644 --- a/packages/function-demethodize/package.json +++ b/packages/function-demethodize/package.json @@ -3,6 +3,7 @@ "version": "3.0.1", "description": "turn a method into a standalone function; the first arg becomes `this`", "main": "index.js", + "types": "index.d.ts", "module": "index.mjs", "exports": { ".": {