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

MatchingStrategies type issues #1588

Closed
flevi29 opened this issue Sep 27, 2023 · 1 comment · Fixed by #1593
Closed

MatchingStrategies type issues #1588

flevi29 opened this issue Sep 27, 2023 · 1 comment · Fixed by #1593
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@flevi29
Copy link
Collaborator

flevi29 commented Sep 27, 2023

I know this might sound like nitpicking, but I am trying to contribute, and this does cause a little issue.
I see that the instantsearch plugin even went so far as to re-implement the native enum into a typescript enum.

export const MatchingStrategies = {
ALL: 'all',
LAST: 'last',
}
export type MatchingStrategies = typeof MatchingStrategies[keyof typeof MatchingStrategies]

Should be re-written to:

export const MatchingStrategies = { 
   ALL: 'all', 
   LAST: 'last', 
 } as const

// OR

export const MatchingStrategies = Object.freeze({ 
   ALL: 'all', 
   LAST: 'last', 
 })
  
 export type MatchingStrategies = typeof MatchingStrategies[keyof typeof MatchingStrategies]

Otherwise MatchingStrategies is just string.

@flevi29 flevi29 changed the title MatchingStrategies re-implemented in instant-meilisearch, because exported type is not very useful MatchingStrategies type issues Sep 27, 2023
@bidoubiwa bidoubiwa added bug Something isn't working enhancement New feature or request and removed bug Something isn't working labels Sep 28, 2023
@curquiza
Copy link
Member

Good idea! Feel free to implement it, we are open to review your PR 😄

@curquiza curquiza added the good first issue Good for newcomers label Sep 28, 2023
@jonespen jonespen mentioned this issue Oct 17, 2023
3 tasks
meili-bors bot added a commit that referenced this issue Oct 17, 2023
1593: Fix const enum issues r=brunoocasali a=jonespen

# Pull Request

## Related issue
Fixes #1588

## What does this PR do?
`const enum` is not really suited for libraries (ref https://youtu.be/jjMbPt_H3RQ?feature=shared&t=249 and https://www.typescriptlang.org/docs/handbook/enums.html#const-enum-pitfalls) and cause issues in typescript projects with `isolatedModules: true` (like Next.js). Instead, regular objects can be used (like done in #1350) with `as const` to make them type safe. 

About the `as readonly string[]` in cfcda4f, this could probably have been solved by a type guard as well, but I opted for the type cast option instead to avoid runtime code changes. 

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


Co-authored-by: Jon Espen Kvisler <jonespen@gmail.com>
meili-bors bot added a commit that referenced this issue Oct 17, 2023
1593: Fix const enum issues r=brunoocasali a=jonespen

# Pull Request

## Related issue
Fixes #1588

## What does this PR do?
`const enum` is not really suited for libraries (ref https://youtu.be/jjMbPt_H3RQ?feature=shared&t=249 and https://www.typescriptlang.org/docs/handbook/enums.html#const-enum-pitfalls) and cause issues in typescript projects with `isolatedModules: true` (like Next.js). Instead, regular objects can be used (like done in #1350) with `as const` to make them type safe. 

About the `as readonly string[]` in cfcda4f, this could probably have been solved by a type guard as well, but I opted for the type cast option instead to avoid runtime code changes. 

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


Co-authored-by: Jon Espen Kvisler <jonespen@gmail.com>
@meili-bors meili-bors bot closed this as completed in da2fbc0 Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants