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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

TFunction types, doesn't infer nested interpolation correctly #2014

Closed
reckter opened this issue Aug 6, 2023 · 15 comments 路 Fixed by #2140 路 May be fixed by Opetushallitus/kouta-ui#573
Closed

TFunction types, doesn't infer nested interpolation correctly #2014

reckter opened this issue Aug 6, 2023 · 15 comments 路 Fixed by #2140 路 May be fixed by Opetushallitus/kouta-ui#573
Assignees

Comments

@reckter
Copy link

reckter commented Aug 6, 2023

馃悰 Bug Report

The TFunction types, do not correctly allow nested interpolation objects.

Example

t("texts.your-browser-supported", {
	browser: {
		family: "family",
		version: "version,
	}
})

will not work, it will produce the error:

 Argument of type '["texts.your-browser-unsupported", { browser: { family: string; version: string; }; }]' is not assignable to parameter of type '[key: "texts.your-browser-unsupported" | "texts.your-browser-unsupported"[], options?: ({ readonly browser: { family: string; version: string; }; } & InterpolationMap<"Your browser ({{browser.family}} {{browser.version}}) is not supported.">) | undefined] | [key: ...] | [key: ...]'.
  Type '["texts.your-browser-unsupported", { browser: { family: string; version: string; }; }]' is not assignable to type '[key: string | string[], defaultValue: string, options?: ({ readonly browser: { family: string; version: string; }; } & $Dictionary) | undefined]'.
    Type at position 1 in source is not compatible with type at position 1 in target.
      Type '{ browser: { family: string; version: string; }; }' is not assignable to type 'string'.

38       {t("texts.your-browser-unsupported", {
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
39        browser: browser,
   ~~~~~~~~~~~~~~~~~~~~~~~~
40       })}
   ~~~~~~~

but with

t("texts.your-browser-supported", {
	"browser.family": "browser.family",
	"browser.version": "browser.version",
})

it will work.

To Reproduce

Codesandbox, with the code above: https://codesandbox.io/s/react-i18next-typescript-code-sandbox-forked-gg6x7q?file=/src/App.tsx

@adrai
Copy link
Member

adrai commented Aug 6, 2023

Probably we need some flatting of the passed interpolation object somewhere here: https://github.com/i18next/i18next/blob/master/typescript/t.d.ts#L133

@adrai
Copy link
Member

adrai commented Aug 6, 2023

feel free to try a PR... else we need to wait for @pedrodurek to have a look at it

@giovannipiller
Copy link

@reckter can you double check with the latest version? Right now it's 23.4.4

Yesterday I forked your example to make some tests for #2016 and today I noticed it didn't show any TS error when upgrading its dependencies.
https://codesandbox.io/s/react-i18next-typescript-code-sandbox-forked-9pcnl8?file=/src/App.tsx

@reckter
Copy link
Author

reckter commented Aug 11, 2023

@giovannipiller I still have the same issue in my project. (originaly with 23.4.1, but also with 23.4.4)

I forked your sandbox again (fork-caption), and it seems it doesn't show errors at all even if I supply completely the wrong object altogether: https://codesandbox.io/s/react-i18next-typescript-code-sandbox-forked-ktvtd3?file=/src/App.tsx
(or missing keys, so I suspect something else is off here.)

@reckter
Copy link
Author

reckter commented Aug 11, 2023

I had a quick look at the changes in 23.4.0 (first version, where the sandbox doesn't work anymore)
and my guess would be at this change: https://github.com/i18next/i18next/pull/2006/files#diff-7aa4473ede4abd9ec099e87fec67fd57afafaf39e05d493ab4533acc38547eb8R926

But without knowing the ins and outs of that section, it's just a guess. Maybe that helps :)

@adrai
Copy link
Member

adrai commented Aug 11, 2023

I had a quick look at the changes in 23.4.0 (first version, where the sandbox doesn't work anymore)

and my guess would be at this change: https://github.com/i18next/i18next/pull/2006/files#diff-7aa4473ede4abd9ec099e87fec67fd57afafaf39e05d493ab4533acc38547eb8R926

But without knowing the ins and outs of that section, it's just a guess. Maybe that helps :)

@pedrodurek possible?

@stale
Copy link

stale bot commented Sep 17, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Sep 17, 2023
@adrai adrai removed the stale label Sep 17, 2023
@pedrodurek
Copy link
Member

I'll look into it this week

@OleksiiKachan
Copy link

any updates on this?

Copy link

stale bot commented Dec 15, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Dec 15, 2023
@adrai adrai removed the stale label Dec 15, 2023
@adrai
Copy link
Member

adrai commented Feb 17, 2024

@marcalexiei do you have an idea why this behaves like that?

@marcalexiei
Copy link
Member

marcalexiei commented Feb 17, 2024

InterpolationMap provides the union of strings wrapped between
CustomOptions#InterpolationPrefix and CustomOptions#InterpolationSuffix from a given string.

i18next/typescript/t.d.ts

Lines 137 to 146 in 2f3384b

type ParseInterpolationValues<Ret> =
Ret extends `${string}${_InterpolationPrefix}${infer Value}${_InterpolationSuffix}${infer Rest}`
?
| (Value extends `${infer ActualValue},${string}` ? ActualValue : Value)
| ParseInterpolationValues<Rest>
: never;
type InterpolationMap<Ret> = Record<
$PreservedValue<ParseInterpolationValues<Ret>, string>,
unknown
>;


E.g.:

image

To solve the issue the code of InterpolationMap should be updated to generate mapped types from the keys.


I can try to setup a PR to fix this issue in the upcoming days

@adrai
Copy link
Member

adrai commented Feb 17, 2024

Do you think this will slow down the performance a lot?
Can you prepare a PR?

@marcalexiei
Copy link
Member

Do you think this will slow down the performance a lot?

I don't think so, but I'll try to make some test

Can you prepare a PR?

Yes, I'll try to do it in the upcoming days

@adrai
Copy link
Member

adrai commented Feb 18, 2024

should be addressed with v23.9.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants