From 1e12476765c61d56ae6bb5571dbd9f84101d5c70 Mon Sep 17 00:00:00 2001 From: Thomas Clark <48699309+TClark1011@users.noreply.github.com> Date: Mon, 1 Aug 2022 08:39:06 +1000 Subject: [PATCH] Add typings for function-demethodize (#469) Co-authored-by: Thomas Clark --- packages/function-demethodize/index.d.ts | 13 ++++++++++++ packages/function-demethodize/index.tests.ts | 22 ++++++++++++++++++++ packages/function-demethodize/package.json | 1 + 3 files changed, 36 insertions(+) create mode 100644 packages/function-demethodize/index.d.ts create mode 100644 packages/function-demethodize/index.tests.ts 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": { ".": {