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 const enums when the --isolateModules flag is provided #1350

Closed
jimmyruann opened this issue Oct 9, 2022 · 3 comments · Fixed by #1358
Closed

MatchingStrategies const enums when the --isolateModules flag is provided #1350

jimmyruann opened this issue Oct 9, 2022 · 3 comments · Fixed by #1358
Labels
bug Something isn't working

Comments

@jimmyruann
Copy link
Contributor

Description
I am trying to import MatchingStrategies into my next app and got this error. I understand that disabling --isolateModules will resolve the problem but Next.Js keep reverting tsconfig back.

Expected behavior
No isolateModules error.

Current behavior
Getting this error Cannot access ambient const enums when the '--isolatedModules' flag is provided.

Screenshots or Logs
image
image

Environment (please complete the following information):

  • OS: Ubuntu 22.02 WSL
  • Meilisearch version: v0.28.1
  • meilisearch-js version: v0.28.0
  • Browser: Chrome 105.0.5195.127 (Official Build) (64-bit) (cohort: Stable)

I found an alternative way to define the enum as i came across this problem before.
Thank you

@bidoubiwa
Copy link
Contributor

Hey @jimmyruann
There is actually an issue with exporting enums from a library. I'll try to explain that better ASAP :)

@bidoubiwa bidoubiwa added the bug Something isn't working label Oct 10, 2022
@jimmyruann
Copy link
Contributor Author

jimmyruann commented Oct 11, 2022

I did something like this, shall I raise PR?

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

export type MatchingStrategies = typeof MatchingStrategies[keyof typeof MatchingStrategies]

Source: https://github.com/jimmyruann/meilisearch-js/commit/4f444ea366da4700af713b6563a44405c621ee4f

@bidoubiwa
Copy link
Contributor

Yes @jimmyruann, I will try it between my typescript projects. I read a bit online and this seems a valid solution.
Thanks a lot

@meili-bors meili-bors bot closed this as completed in bd914b8 Oct 13, 2022
@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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants