Skip to content

Commit

Permalink
Add typings for function-demethodize (#469)
Browse files Browse the repository at this point in the history
Co-authored-by: Thomas Clark <[email protected]>
  • Loading branch information
TClark1011 and Thomas Clark authored Jul 31, 2022
1 parent 48a1ad4 commit 1e12476
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/function-demethodize/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
declare function demethodize<Entity, MethodName extends keyof Entity>(
method: Entity[MethodName] & Function
): Entity[MethodName] extends (...p: any[]) => any
? (
p: Entity,
...args: Parameters<Entity[MethodName]>
) => ReturnType<Entity[MethodName]>
: never;
declare function demethodize<Entity, Params extends any[], Returns>(
method: (...p: Params) => Returns
): (p: Entity, ...a: Params) => Returns;

export default demethodize;
22 changes: 22 additions & 0 deletions packages/function-demethodize/index.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import demethodize from "./index";

//OK

const test1: (p: string) => string = demethodize("".trim);
const test2 = demethodize<string, "big">("".big);
const test3 = demethodize<number, "toFixed">((1).toFixed);
const test4 = demethodize<Object, "toString">({}.toString);
const test5 = demethodize<any[], "map">([].map);
const test6 = demethodize<string, [string], string[]>("".split);

// Not OK
// @ts-expect-error
demethodize<string, "trim">();
// @ts-expect-error
demethodize<number, "valueOf">((1).toExponential);
// @ts-expect-error
demethodize<boolean, "valueOf">([].toString);
// @ts-expect-error
demethodize<number[], "length">([].length);
// @ts-expect-error
demethodize<string>("".trim);
1 change: 1 addition & 0 deletions packages/function-demethodize/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
".": {
Expand Down

0 comments on commit 1e12476

Please sign in to comment.