-
-
Notifications
You must be signed in to change notification settings - Fork 652
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
Add brand to TFunction type so different namespaces' TFunctions are not treated as compatible #1994
Conversation
…ot treated as compatible
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.
included in v23.2.9 |
Hi, since this change how we should/could type previous versions, worked as a charm: import { TFunction } from "i18next";
export interface ButtonProps {
t: TFunction;
title: string;
} usage: const { t } = useTranslation(["action_sheets", "common"]);
<Button
t={t}
title={t("button.close", { ns: "common" })}
/> In
|
I have a similar issue to @efstathiosntonas. note that since "bar" comes before "foo" in the array ordering, it is considered the default namespace for the instance of t and it could be safely passed to a function as below: function bar(t: TFunction<"bar">) {
// inside bar() t is constrained to only use translation keys from the "bar" namespace
}
//
const { t } = useTranslation(["bar", "foo"]);
bar(t); // should be correct, but gives error in v23.2.10 However, if "foo" comes first in the array order then this wouldn't work because namespace prefixes would need to be used inside bar() in order to access it's values. function bar(t: TFunction<"bar">) {
// inside bar() t is constrained to only use translation keys from the "bar" namespace
}
const { t } = useTranslation(["foo", "bar"]);
bar(t); // should be an error It gets even more complicated with default namespaces: function bar(t: TFunction<"bar">) {
// inside bar() t is constrained to only use translation keys from the "bar" namespace
}
const { t } = useTranslation();
bar(t); // should be an error if "bar" is not the default namespace There are other examples to consider as well function bar(t: TFunction<["bar"]>) {
}
const { t } = useTranslation("bar");
bar(t); There are other examples to consider as well function bar(t: TFunction<["bar", "foo"]>) {
}
const { t } = useTranslation(["foo", "bar"]);
bar(t); We need to update the test cases to check all of these. |
@jtbandes can you have a look at this please? |
I believe we should deprecate const { t } = useTranslation();
const { t: tOther } = useTranslation('other'); If we did want to support loading multiple namespaces in a single const { t } = useTranslation(['a', 'b']); // order doesn't matter
const value = t('a:label'); // must always use `a:` there is no default |
@efstathiosntonas can you try with this? import { TFunction } from "i18next";
export interface ButtonProps {
t: TFunction<["action_sheets", "common"]>;
title: string;
} |
How about this? #1997 |
fyi: v23.2.11 should optimize this |
Does anyone face with type error after this merged? In case i want to pass TFunction as parameter:
Property '$TFunctionBrand' is missing in type 'TFunction<Namespaces.SomeNamespace[], undefined>' but required in type 'TFunction<any, any>'.ts(2345) |
Make sure you use the newest i18next version and remove the any, any generics |
The problem has appeared after new version released ( package.json has "i18next": "^23.2.2").
this is the error message (i've replaced project path with ****)
|
@jokerosky can you provide a reproducible github repo? |
Strange, in fresh project there is no error https://github.com/jokerosky/i18next-types-bug-sample/blob/main/src/App.tsx#L17 will check my configuration and other packages. Thank you for the quick response and attention ❤️ |
Does anyone face this type issue after upgrading to v23.2.11?
|
@jtbandes can you help @souvikjanatw ? |
Can you use |
@jtbandes It's giving the following error: As it says
|
Previously, two
TFunction
types would be treated as compatible even if they referred to different namespaces. For example:This PR adds a property to the
TFunction
interface, making it a "branded" type. TwoTFunction
s with different values ofNs
will no longer be treated as compatible. The example above will now fail to compile.Fixes #1941