Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typings for function-demethodize #469

Merged
merged 1 commit into from
Jul 31, 2022

Conversation

TClark1011
Copy link
Contributor

@TClark1011 TClark1011 commented Jul 13, 2022

I think this is the last package that needed typings.

Currently there are 2 signatures you can call this with:

demethodize<string, [string], string>("".split);
//OR
demethodize<string, "split">("".split);

The best way to use this in my opinion is to specify the function typing on the variable you assign the function too, as such:

const split: (p:string, a:string) => string[] = demethodize("".split)

Unfortunately, you have to manually pass generics to this function in order to use it (unless you use the method shown directly above). This is because when you call demethodize("".split) typescript has no way of knowing what type you called the .split method on.

I wasn't able to get the generics working exactly how I wanted to, ideally, you would only have to specify the type that you are pulling the method from, and then it could auto infer the rest, eg;

demethodize<string>("".split)

Would infer the function signature (p:string, a:string) => string[], however, I don't think that is currently possible because of the way that type inference works with default values for generics.

Edit: I just noticed that my prettier config may have messed with the formatting of the files, I wasn't able to run eslint to format any typescript files. Seems like eslint is not configured to fix the typescript files.

@angus-c
Copy link
Owner

angus-c commented Jul 18, 2022

Thanks for taking the time on this difficult one. I think this is the best solution, but would like to think about this a little more.

@angus-c angus-c merged commit 1e12476 into angus-c:master Jul 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants