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

Rework types to allow Parameters<typeof filesize> to function properly #180

Merged
merged 2 commits into from
Mar 21, 2024

Conversation

abaltuta
Copy link
Contributor

I have a usage in a codebase that relies on the types of the parameters. Initially our code looked something like:

type Foo = Parameters<typeof filesize>[1];

However since this change (#172) change that is no longer possible. There is a larger thread in the typescript repo about this.

This PR reworks the types so that they are automatically inferred by the type of options passed and avoids overloads.

An alternative to this would be export all the possible option types and return types, but you can get the same result using Parameters and ReturnType utility types.

TypeScript Playground Link

@abaltuta abaltuta changed the title Rework types to allow usages like Parameters<typeof filesize> to function properly Rework types to allow Parameters<typeof filesize> to function properly Mar 18, 2024
@avoidwork
Copy link
Owner

avoidwork commented Mar 20, 2024

Thanks for the PR! Is this backwards compatible, and if so, would this be a minor or patch* version bump?

@avoidwork
Copy link
Owner

avoidwork commented Mar 20, 2024

The partial playground examples aren't correct; the function takes the second parameter and returns a partial application. The number is then passed to the return function for the second parameter's application.

They'd be like this:

const defaultReturnPartial = partial();  // this one does nothing, it's defaults
const arrayReturnPartial = partial({ output: "array"});
const exponentReturnPartial = partial({ output: "exponent"});
const stringReturnPartial = partial({ output: "string"});
const objectReturnPartial = partial({ output: "object"});

those functions would then receive a number, e.g. 1024, & return the expected outputs.

@abaltuta
Copy link
Contributor Author

abaltuta commented Mar 20, 2024

@avoidwork I'm sorry. You are correct. I should not open PRs at 3AM.

I've fixed the issue. Here is another playground to show how these work. Use the link below since it contains the old types as well.

Is this backwards compatible, and if so, would this be a minor or patch* version bump?

I'd say this is a patch. But I'm going to be completely honest, I didn't try other examples other than my codebase where I got the desired behavior when compared to the existing types.

@abaltuta
Copy link
Contributor Author

From what I see there shouldn't be any difference.
I've expressed the same thing in TypeScript only I've used a discriminated union (thanks to the output key) rather than overloads.
I am unsure how others use this package's types, if anything this should be better since overloads have some limitations, but 99% of users will feel nothing.

Here is yet another playground where I also added the existing types to show that they produce the same results.

@avoidwork avoidwork merged commit 4268a4e into avoidwork:master Mar 21, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants