-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add typings for function-demethodize (#469)
Co-authored-by: Thomas Clark <[email protected]>
- Loading branch information
1 parent
48a1ad4
commit 1e12476
Showing
3 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters