-
-
Notifications
You must be signed in to change notification settings - Fork 1k
fix trans component type safe #1881
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
base: master
Are you sure you want to change the base?
Conversation
… value in test environment" i18next#1883 by also respecting i18next#1876
|
@ahrjarrett @marcalexiei @adrai |
|
lgtm but @marcalexiei or some other TS expert should also have a look at this... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adrai copying these four types directly from i18next isn’t ideal, IMHO, as it would increase maintenance overhead:
Keeping these types in sync across both repositories would be necessary to ensure consistent behaviour,
and that might become harder to manage if other plugins need this feature in the future.
Ideally, i18next itself should document that these four types are used externally and need to stay aligned.
That said, if maintaining these types across repos is acceptable to you, feel free to proceed.
Alternatively, a cleaner solution might be to export InterpolationMap directly from i18next and reuse it here.
If you go with this approach, don’t forget to update the semver peerDependencies#i18next version in package.json.
@codomposer can you update the PR (rebase with master), to make use of |
I've updated the PR, can you please check it? |
|
please update the PR to only include the relevant changes.... |
|
fixed test failures |
|
@marcalexiei can you please check if it's better like that? |
| * Distributive version of InterpolationMap that works correctly with union types | ||
| * This ensures each key is processed individually rather than creating an intersection | ||
| */ | ||
| type TransInterpolationMap<Ns extends Namespace, Key, TOpt extends TOptions> = Key extends any |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Key extends any is always true since all types extend any.
Is this constraint / type necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added it as a fix for test failures,
without it, users would need to provide all interpolation variables from all possible keys
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the type is exported but doesn't parse {{variable}} syntax from translation strings, and can't provide intellisense.
to enable that, I think I should create a custom type that extracts interpolation variables from translation strings or I will modify the comment, let me know your thoughts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The purpose of the PR is to make Trans components type-safe.
Based on this I suppose that at least one of the following behaviours on values prop:
-
(This is mandatory IMHO) Right now values is typed as a top type (
anyorunknown) this allows any type to be provided which is something that I do not consider safe from a type perspective.

-
(Nice to have) autocomplete on
valueskeys (CustomTypeOptions['resources']areconststrings),
as per my previous screenshot this is not happening.
No key is suggested, if this is not feasible the comment on test is misleading and should be changed. -
(Nice to have) if I pass an invalid
valueskey typescript should report an error... right now any key is accepted.

If I'm not mistaken this feature is present for tFunction so if additional logic from the core is needed to achieve it should be exported from i18next.
Besides I think you should also add test cases for the selector API created by @ahrjarrett.
|
@marcalexiei |
| type ValuesType = TitleProps['values']; | ||
|
|
||
| // @ts-expect-error - empty object should not be assignable when appName is required | ||
| const invalidValues: ValuesType = {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| const invalidValues: ValuesType = {}; | |
| assertType<ValuesType>({}); |
You need to add assertType in vitest import
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Testing this in a real app:
There is an issue with string (import from json) + selector API and context:
import { Trans } from "react-i18next";
export function TranCmp() {
const someCondition = true;
return (
<div>
<Trans
ns="ns1"
i18nKey={($) => $.food}
context="pizza"
values={{ test: "a" }}
/>
<Trans
ns="ns1"
i18nKey={($) => $.food}
context={someCondition ? "pizza" : undefined}
values={{ test: "a" }}
/>
</div>
);
}with this translation file:
{
"food": "food",
"food_pizza": "pizza"
}
You can find the code here: https://github.com/marcalexiei/i18next-playground/tree/main/examples/react-trans-component-type-safe-selector
Type '{ test: string; }' is not assignable to type 'undefined'.
The expected type comes from property 'values' which is declared here on type 'IntrinsicAttributes & TransSelectorProps<SelectorFn<{ prova: string; prova_other: string; job: string; job_details: { title: string; }; food: string; food_pizza: string; }, string, { context: "pizza"; }>, "ns1", undefined, "pizza", { ...; }> & HTMLProps<...>'




Fixes
Closes #1772
Checklist
npm run testChecklist (for documentation change)
Contribution by Gittensor, learn more at https://gittensor.io/