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: export ts types from detect-locale #1742

Merged
merged 1 commit into from
Aug 10, 2023
Merged

Conversation

nadavami
Copy link
Contributor

@nadavami nadavami commented Aug 9, 2023

Description

Fixes a bug where typescript types for the detect-locale package aren't being exported. Currently using detect-locale in a TS project throws the following error:

Could not find a declaration file for module '@lingui/detect-locale'. '.../@lingui+detect-locale@4.3.0/node_modules/@lingui/detect-locale/dist/index.mjs' implicitly has an 'any' type.
  There are types at '.../node_modules/@lingui/detect-locale/dist/index.d.ts', but this result could not be resolved when respecting package.json "exports". The '@lingui/detect-locale' library may need to update its package.json or typings.ts(7016)

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

Fixes # (issue)

Checklist

  • I have read the CONTRIBUTING and CODE_OF_CONDUCT docs
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

@vercel
Copy link

vercel bot commented Aug 9, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
js-lingui ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 9, 2023 5:03pm

@github-actions
Copy link

github-actions bot commented Aug 9, 2023

size-limit report 📦

Path Size
./packages/core/dist/index.mjs 2.76 KB (0%)
./packages/detect-locale/dist/index.mjs 721 B (0%)
./packages/react/dist/index.mjs 1.66 KB (0%)
./packages/remote-loader/dist/index.mjs 7.24 KB (0%)

@codecov
Copy link

codecov bot commented Aug 9, 2023

Codecov Report

Patch and project coverage have no change.

Comparison is base (8114a69) 75.95% compared to head (69ab80b) 75.95%.
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1742   +/-   ##
=======================================
  Coverage   75.95%   75.95%           
=======================================
  Files          80       80           
  Lines        2063     2063           
  Branches      529      529           
=======================================
  Hits         1567     1567           
  Misses        383      383           
  Partials      113      113           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@andrii-bodnar andrii-bodnar left a comment

Choose a reason for hiding this comment

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

@nadavami thank you!

@andrii-bodnar andrii-bodnar merged commit 4af4448 into lingui:main Aug 10, 2023
@nadavami
Copy link
Contributor Author

My pleasure, thanks for the great lib!

sentience added a commit to cultureamp/js-lingui that referenced this pull request Aug 24, 2023
The recent lingui#1742 explicitly exports TypeScript type definitions
from package.json:

```json
  "exports": {
     ".": {
       "require": {
         "types": "./dist/index.d.ts",
         "default": "./dist/index.cjs"
       },
       "import": {
         "types": "./dist/index.d.ts",
         "default": "./dist/index.mjs"
       }
     },
     "./package.json": "./package.json"
   },
```

Unfortunately, [as documented in the TypeScript Handbook][handbook],
CommonJS (`require`) and ESM (`import`) module versions
cannot share the same index.d.ts file:

> It’s important to note that the CommonJS entrypoint and the ES module entrypoint each needs its own declaration file, even if the contents are the same between them. Every declaration file is interpreted either as a CommonJS module or as an ES module, based on its file extension and the "type" field of the package.json, and this detected module kind must match the module kind that Node will detect for the corresponding JavaScript file for type checking to be correct. Attempting to use a single .d.ts file to type both an ES module entrypoint and a CommonJS entrypoint will cause TypeScript to think only one of those entrypoints exists, causing compiler errors for users of the package.

I am seeing the compiler errors that this documentation warns of.

This issue appears to have been recently fixed in
the unbuild build tool that js-lingui depends upon
(see issue unjs/unbuild#238
and fix unjs/unbuild#273),
and that fix was released in [unbuild 2.0.0-rc0].

This fix for detect-locale, therefore, is to upgrade to unbuild 2.0.0,
and use the separate .d.cts and .d.mts type declaration files
it outputs.

**Note:** This moves detect-locale onto a newer major version of unbuild
than the other packages in this monorepo.
It may be preferred to upgrade them all,
but I am not familiar enough with the other packages to do this quickly.

[handbook]: https://www.typescriptlang.org/docs/handbook/esm-node.html#packagejson-exports-imports-and-self-referencing
[unbuild 2.0.0-rc0]: https://github.com/unjs/unbuild/blob/main/CHANGELOG.md#v200-rc0
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