Skip to content

Commit 61fe80c

Browse files
committed
fix(types): values prop on <Trans /> now only requires interpolation variables for the specific i18nKey, not all variables in the namespace #1913
1 parent b58d5a5 commit 61fe80c

File tree

5 files changed

+53
-11
lines changed

5 files changed

+53
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 17.0.2
2+
3+
- fix(types): `values` prop on `<Trans />` now only requires interpolation variables for the specific `i18nKey`, not all variables in the namespace [1913](https://github.com/i18next/react-i18next/issues/1913)
4+
15
## 17.0.1
26

37
- chore: bump minimum i18next peer dependency to `>= 26.0.1` _(forgot to do it in last version)_

TransWithoutContext.d.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export type TransProps<
3232
KPrefix = undefined,
3333
TContext extends string | undefined = undefined,
3434
TOpt extends TOptions & { context?: TContext } = { context: TContext },
35+
Ret = TFunctionReturn<Ns, _AppendKeyPrefix<Key, KPrefix>, TOpt>,
3536
E = React.HTMLProps<HTMLDivElement>,
3637
> = E & {
3738
children?: TransChild | readonly TransChild[];
@@ -44,21 +45,26 @@ export type TransProps<
4445
ns?: Ns;
4546
parent?: string | React.ComponentType<any> | null; // used in React.createElement if not null
4647
tOptions?: TOpt;
47-
values?: InterpolationMap<TFunctionReturn<Ns, _AppendKeyPrefix<Key, KPrefix>, TOpt>>;
48+
values?: InterpolationMap<Ret>;
4849
shouldUnescape?: boolean;
4950
t?: TFunction<Ns, KPrefix>;
5051
};
5152

5253
export interface TransLegacy {
5354
<
54-
Key extends ParseKeys<Ns, TOpt, KPrefix>,
55+
const Key extends ParseKeys<Ns, TOpt, KPrefix>,
5556
Ns extends Namespace = _DefaultNamespace,
5657
KPrefix = undefined,
5758
TContext extends string | undefined = undefined,
5859
TOpt extends TOptions & { context?: TContext } = { context: TContext },
60+
Ret extends TFunctionReturn<Ns, _AppendKeyPrefix<Key, KPrefix>, TOpt> = TFunctionReturn<
61+
Ns,
62+
_AppendKeyPrefix<Key, KPrefix>,
63+
TOpt
64+
>,
5965
E = React.HTMLProps<HTMLDivElement>,
6066
>(
61-
props: TransProps<Key, Ns, KPrefix, TContext, TOpt, E>,
67+
props: TransProps<Key, Ns, KPrefix, TContext, TOpt, Ret, E>,
6268
): React.ReactElement;
6369
}
6470

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
"eslint-plugin-testing-library": "^6.5.0",
128128
"happy-dom": "^20.8.9",
129129
"husky": "^9.1.7",
130-
"i18next": "^26.0.1",
130+
"i18next": "^26.0.3",
131131
"lint-staged": "^16.4.0",
132132
"mkdirp": "^3.0.1",
133133
"prettier": "^3.8.1",
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { describe, it, assertType } from 'vitest';
2+
import * as React from 'react';
3+
import { Trans, useTranslation } from 'react-i18next';
4+
5+
/**
6+
* Regression test for https://github.com/i18next/react-i18next/issues/1913
7+
*
8+
* The `values` prop of `Trans` was requiring ALL interpolation variables from
9+
* the entire namespace instead of only the variables for the specific `i18nKey`.
10+
*/
11+
describe('issue #1913 - values prop should only require variables for the given key', () => {
12+
it('should work in JSX without t', () => {
13+
assertType<React.ReactElement>(<Trans i18nKey="title" values={{ appName: 'My App' }} />);
14+
});
15+
16+
it('should work in JSX with t from useTranslation', () => {
17+
const { t } = useTranslation();
18+
assertType<React.ReactElement>(<Trans t={t} i18nKey="title" values={{ appName: 'My App' }} />);
19+
});
20+
21+
it('should work in JSX with t from array namespace', () => {
22+
const { t } = useTranslation(['custom']);
23+
assertType<React.ReactElement>(<Trans t={t} i18nKey="title" values={{ appName: 'My App' }} />);
24+
});
25+
26+
it('should work in JSX with explicit ns', () => {
27+
const { t } = useTranslation(['custom']);
28+
assertType<React.ReactElement>(
29+
<Trans t={t} ns="custom" i18nKey="title" values={{ appName: 'My App' }} />,
30+
);
31+
});
32+
});

0 commit comments

Comments
 (0)