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

compileNamespace: "ts" does not correctly type imports #1651

Closed
renchap opened this issue May 13, 2023 · 5 comments
Closed

compileNamespace: "ts" does not correctly type imports #1651

renchap opened this issue May 13, 2023 · 5 comments

Comments

@renchap
Copy link
Contributor

renchap commented May 13, 2023

Describe the bug
Currently, when you configure compileNamespace: "ts" then Lingui will create both a app.ts and app.d.ts files.

But Typescript does ignore files with the same base name and different extension, as described here: https://typescript-eslint.io/architecture/parser/#project

TypeScript will ignore files with duplicate filenames in the same folder (for example, src/file.ts and src/file.js). TypeScript purposely ignore all but one of the files, only keeping the one file with the highest priority extension (the extension priority order (from highest to lowest) is .ts, .tsx, .d.ts, .js, .jsx). For more info see #955.

This makes Typescript ignore your .d.ts file and type the import as any.

You also get this error when using typescript-eslint and you open a Lingui-generated .d.ts file:

Parsing error: ESLint was configured to run on <tsconfigRootDir>/locales/fr/core.d.ts using parserOptions.project: $project/packages/core/tsconfig.json
However, that TSConfig does not include this file. Either:

To Reproduce
In a typescript project, configure Lingui with compileNamespace: "ts", compile a catalog, then import it from a Typescript file.

The import wont be typed (any type).

Expected behavior
The catalog is correctly typed

Additional context

  • jsLingui version lingui --version: 4.0.0

Suggested fix
The correct behaviour here would be either:

  • generate an app.js and an app.d.ts. Then Typescript will correctly use the d.ts file to type imports from the messages catalog
  • generate only an app.ts file, which includes the types information. Those will be stripped off when transpiled, so it wont change the resulting file. It can look like this:
/*eslint-disable*/
import { Messages } from '@lingui/core';
export const messages: Messages=JSON.parse();

I personally think the second solution is much cleaner and idiomatic. You are asking for Typescript code to get generated after all :)

@thekip
Copy link
Collaborator

thekip commented May 15, 2023

I personally think there is no big value of compileNamespace: "ts" because in most of the cases you would use dynamic import such as import('/locales/' + locale + '.po') which will not be typed any way. And even if typed it directly passed to the i18n functions, so you will not benefit from typings here.

With that being said, I'm not fascinated by these changes myself, so contributions are very welcome.

@renchap
Copy link
Contributor Author

renchap commented May 15, 2023

If you are fine with me switching the ts compile to outputting an unique TS file with the types embedded, I can send a PR for this.

@thekip
Copy link
Collaborator

thekip commented May 15, 2023

@renchap i think it's fine.

@ptmkenny
Copy link

Any progress with this? I ran into this today when scanning my code with type-coverage. I agree with the second solution suggested.

@renchap
Copy link
Contributor Author

renchap commented Feb 20, 2024

Fixed by #1851

@renchap renchap closed this as completed Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants