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

fix(types): make all usages of UiState in InstantSearch generic #5060

Merged
merged 3 commits into from
Jun 9, 2022

Conversation

Haroenv
Copy link
Contributor

@Haroenv Haroenv commented Jun 8, 2022

Summary

This allows someone with custom widgets to still use setUiState etc. without type errors

Result

all places UiState is used in the InstantSearch instance are now generic and use TUiState.

There are still places left in individual widgets, middleware which aren't generic, but that can be solved in a different PR

A sandbox showing the fixed problem: https://codesandbox.io/s/instantsearch-js-forked-m8nm7s?file=/src/app.ts:1152-1262

This allows someone with custom widgets to still use setUiState etc. without type errors
@Haroenv Haroenv requested review from a team, FabienMotte and sarahdayan and removed request for a team June 8, 2022 13:01
@codesandbox-ci
Copy link

codesandbox-ci bot commented Jun 8, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 8d01a07:

Sandbox Source
InstantSearch.js Configuration
InstantSearch.js (forked) PR

@@ -2038,7 +2038,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/instantsear

test('with function form sets indices state', async () => {
const searchClient = createSearchClient();
const search = new InstantSearch({
const search = new InstantSearch<UiState>({
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is needed because initialUiState seems to "fill in" the generic. That makes sense, but in this case it makes things required that aren't actually required. Not sure how to solve that though! This already was the case before the PR, but became visible due to usage of TUiState in setUiState

Copy link
Contributor Author

@Haroenv Haroenv Jun 8, 2022

Choose a reason for hiding this comment

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

#5061 fixes this, but I'll merge this as two PRs

Haroenv added a commit that referenced this pull request Jun 8, 2022
if you pass initialUiState, that should be asserted by the generic UiState, not automatically accepted. Otherwise you get use cases like the "with function form sets indices state" test where setUiState thinks query is required, just because it has the default given

The way this works is by using `NoInfer` from microsoft/TypeScript#14829 (comment) which seems to be quite relied upon

This is a follow-up on #5060
Haroenv added a commit to algolia/react-instantsearch that referenced this pull request Jun 8, 2022
Haroenv added a commit to algolia/react-instantsearch that referenced this pull request Jun 9, 2022
* fix(types): allow useInstantSearch to be generic

* avoid algolia/instantsearch#5060
@Haroenv Haroenv enabled auto-merge (squash) June 9, 2022 11:50
@Haroenv Haroenv merged commit 2b9e76b into master Jun 9, 2022
@Haroenv Haroenv deleted the fix/types-generic branch June 9, 2022 11:54
Haroenv added a commit that referenced this pull request Jun 9, 2022
* fix(types): make all usages of UiState in InstantSearch generic

This allows someone with custom widgets to still use setUiState etc. without type errors

* fix lint

* fix(types): avoid inferring UiState type from initialUiState

if you pass initialUiState, that should be asserted by the generic UiState, not automatically accepted. Otherwise you get use cases like the "with function form sets indices state" test where setUiState thinks query is required, just because it has the default given

The way this works is by using `NoInfer` from microsoft/TypeScript#14829 (comment) which seems to be quite relied upon

This is a follow-up on #5060
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