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

chore: improve types for MapeoProject's kProjectReplicate #900

Merged
merged 3 commits into from
Oct 23, 2024

Conversation

EvanHahn
Copy link
Contributor

@EvanHahn EvanHahn commented Oct 9, 2024

This change should have no user impact.

I recommend reviewing this with whitespace changes disabled.

MapeoProject's kProjectReplicate member will give type errors if called with a boolean. We aren't doing this today, but we will in our upcoming server work.

This happened because TypeScript's Parameters utility type only takes the last signature of an overloaded function. To quote the docs:

When inferring from a type with multiple call signatures (such as the type of an overloaded function), inferences are made from the last signature (which, presumably, is the most permissive catch-all case). It is not possible to perform overload resolution based on a list of argument types.

To fix this, we specify the types manually rather than pull them out of Parameters. Doing this fixes the potential type error.

I also did a small amount of cleanup on the types in this function.

(This change is primarily to fix an error in our upcoming server branch, but I think it's useful on its own.)

*This change should have no user impact.*

*I recommend reviewing this with whitespace changes disabled.*

`MapeoProject`'s `kProjectReplicate` member will give type errors if
called with a boolean. We aren't doing this today, but [we will in our
upcoming server work][0].

This happened because TypeScript's `Parameters` utility type only takes
the *last* signature of an overloaded function. To quote [the docs][1]:

> When inferring from a type with multiple call signatures (such as the
> type of an overloaded function), inferences are made from the last
> signature (which, presumably, is the most permissive catch-all case).
> It is not possible to perform overload resolution based on a list of
> argument types.

To fix this, we specify the types manually rather than pull them out of
`Parameters`. Doing this fixes the potential type error.

I also did a small amount of cleanup on the types in this function.

(This change is primarily to fix an error in our upcoming server branch,
but I think it's useful on its own.)

[0]: https://github.com/digidem/comapeo-core/blob/9aaeb37910821a6779ec8eef88af995e94448ba3/src/mapeo-project.js#L140-L141
[1]: https://www.typescriptlang.org/docs/handbook/2/conditional-types.html
@EvanHahn EvanHahn requested a review from gmaclennan October 9, 2024 21:59
Copy link
Member

@gmaclennan gmaclennan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Bear in mind that this now excludes from types passing a Protomux instance, which is supported, however the return type is different when passing a protomux. I don't think we currently use this method with a protomux instance (although we do use hypercore.replicate(protomux) in our sync API). This is an upstream bug, but I think our types are also incorrect (types say return type is Protomux, but I think the return type is NoiseStream).

@EvanHahn EvanHahn merged commit 100a973 into main Oct 23, 2024
6 checks passed
@EvanHahn EvanHahn deleted the fix-up-kprojectreplicate-types branch October 23, 2024 14:42
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