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

[Typescript] CompiledMessageToken type doesn't play well with imported catalog. #1460

Open
zimme opened this issue Feb 22, 2023 · 10 comments
Open

Comments

@zimme
Copy link

zimme commented Feb 22, 2023

Describe the bug
I get type errors when trying to use my message catalogs.

To Reproduce
For the following code

import { messages as englishMessages } from "./locales/en/messages";
import { messages as swedishMessages } from "./locales/sv/messages";

i18n.loadLocaleData({
  en: { plurals: en },
  sv: { plurals: sv },
});

i18n.load({
  en: englishMessages, // Type errors here
  sv: swedishMessages, // Type errors here
});

...

I get the following type errors

Type '(string | string[])[]' is not assignable to type 'CompiledMessage'.
      Type '(string | string[])[]' is not assignable to type 'CompiledMessageToken[]'.
        Type 'string | string[]' is not assignable to type 'CompiledMessageToken'.
          Type 'string[]' is not assignable to type 'CompiledMessageToken'.
            Type 'string[]' is not assignable to type 'string'.ts(2322)

If I manually change the type CompiledMessageToken I can fix the error.

// Doesn't work
type CompiledMessageToken = string | [name: string, type?: string, format?: null | string | CompiledIcuChoices];

// Works, see the extra string[]
type CompiledMessageToken = string | string[] | [name: string, type?: string, format?: null | string | CompiledIcuChoices];

I believe the problem was introduced here. 88ac94a#diff-7a532cc8360c9c437a7693ba2c8767f39ce5e31dd36759d63a03c33286bb63b6L22-R29

Expected behavior
No type error when using imported catalogs

@zimme
Copy link
Author

zimme commented Feb 22, 2023

Typescript infers the type (string | string[])[] for some of my messages in my catalog.

I believe the message is something like this "Appointment with {0}" that's compiled into ["Appointment with ", ["0"]].

@pablohpsilva-forjnetwork

Can we also get this warning fixed?

warning "workspace-aggregator-c7800d1b-27a7-41b4-b29b-317c787b474d > app > @lingui/[email protected]" has incorrect peer dependency **"typescript@2 || 3 || 4".**

TS released v5 :)

@vddgil
Copy link

vddgil commented Mar 31, 2023

Hello here,

I have the same issue but change the CompiledMessageToken type doesn't work. Here is the error:
Screenshot Teammates | Team | Scribe 2023-03-31 08-59-37

Is any fix planned?

Thanks a lot!

@andrii-bodnar
Copy link
Contributor

Currently, we have the v4 pre-release - https://github.com/lingui/js-lingui/releases

Could you please check if this is still relevant in this version? The latest version for now is 4.0.0-next.4

@vddgil
Copy link

vddgil commented Mar 31, 2023

Exactly the same issue:

Screenshot Teammates | Team | Scribe 2023-03-31 09-27-27

And another issue with @lingui/macro ...

Screenshot Teammates | Team | Scribe 2023-03-31 09-27-35

@vddgil
Copy link

vddgil commented Mar 31, 2023

Actually, maybe it will be fixed if I recompile the messages. But I'm not able to because of the @lingui/macro issue :(

@timofei-iatsenko
Copy link
Collaborator

@vddgil show the snippet which macro could not proceed, please

@vddgil
Copy link

vddgil commented Mar 31, 2023

Hello @thekip,
I deleted my node_module folder and did a fresh yarn install. With that, I got rid of the plugin error.
But I still have the type issue.

Screenshot Teammates | Team | Scribe 2023-03-31 10-38-27

Here is a Gist of the problematic message.js file https://gist.github.com/vddgil/adb44581a952755b5cc1c3ec8cd479fc

Thanks a lot!

@mustafashabib
Copy link

mustafashabib commented Apr 3, 2023

Same issue.

In the very short term I've just changed our src/il8n/setup.ts to

import { i18n } from '@lingui/core';
import { messages as enMessages } from 'locales/en/messages';
import { messages as esMessages } from 'locales/es/messages';
import { en, es } from 'make-plural/plurals';

[
    { locale: 'en', messages: enMessages, plurals: en },
    { locale: 'es', messages: esMessages, plurals: es },
].forEach(({ locale, messages, plurals }) => {
    i18n.loadLocaleData(locale, { plurals });
    // @ts-expect-error Lingui introduced a type error in the CompiledMessage that needs string[] as a possible type of the value in the Record
    i18n.load(locale, messages);
});

i18n.activate('en');

@timofei-iatsenko
Copy link
Collaborator

guys, just cast it to any. Not a big deal. I would not recommend to use // @ts-expect-error because it mute all errors in the line, not only this one you intentionally want to mute.

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

6 participants