-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Build has Error by Type Type 'string' is not assignable to type 'string[]', but enable returnObjects #1217
Comments
//cc @pedrodurek |
Hey @Him-2C, I'll change the default TFunctionResult to accept other types. Right now, if you rely on the type augmentation as in this example here https://github.com/i18next/react-i18next/blob/master/example/react-typescript4.1/namespaces/%40types/react-i18next/index.d.ts, your problem should be gone, because it'll automatically infer the keys and the return type for you. |
@pedrodurek Thank, but I don't use DefaultResources. |
Hey @Him-2C, it should be fixed now in v11.8.2. |
Hello! o/ I am new to this and my question would be why is the |
Hi @pedrodurek, The fix you made has introduced a new problem for me. ProblemThe following code does not work, and produces a type/build error. const { t } = useTranslation("exampleNamespace");
const foo: string = t("exampleKey");
Cause?I think the DocumentationAccording to the documentation (here), the
Though it looks like this can be complicated by some of the configuration options like For example, if the translation key exists in the resource, but is |
Hey guys, actually it was returning |
Hi @pedrodurek, thanks for responding so quickly! :) I've copied your changes from pedrodurek/react-i18next@766103d2 into my local project and I still don't think this is working for me (in every way that For example, if I make some simple components like this: interface IMessageProps {
value: string;
}
const Message = (props: IMessageProps) => {
return <span>{props.value}</span>;
}; const WorkingMessage = () => {
const { t } = useTranslation('exampleNamespace');
// ✅ This works
return <Message value={t('exampleKey')}/>;
};
const BrokenMessage = () => {
const { t } = useTranslation('exampleNamespace');
const message = t('exampleKey');
// ❌ This throws a type/build error
return <Message value={message} />;
}; It causes this type error:
|
@BenJenkinson thanks for flagging that, yeah I didn't open the PR yet because I'm figuring out a way to default string considering the new types, right now it's defaulting TFunctionResult. In your case, it'd work like this:
which would infer based on the message type |
@pedrodurek, does this work? export type TFuncReturn<TResult extends TFunctionResult = string, N, P, T = Resources> = N extends (keyof T)[]
? NormalizeMultiReturn<T, P>
: N extends keyof T
? NormalizeReturn<T[N], P>
: TResult;
export interface TFunction<TResult extends TFunctionResult = string, N extends Namespace = DefaultNamespace> {
<
K extends TFuncKey<N> | TemplateStringsArray,
R extends TFuncReturn<TResult, N, K>,
I extends object = StringMap
>(
key: K | K[],
options?: TOptions<I> | string,
): R;
<
K extends TFuncKey<N> | TemplateStringsArray,
R extends TFuncReturn<TResult, N, K>,
I extends object = StringMap
>(
key: K | K[],
defaultValue?: string,
options?: TOptions<I> | string,
): R;
} That whole file has more complicated types than I've had to write before, so this might not be correct, but it seems to solve my immediate problem. It allows |
@BenJenkinson Thanks!!! I was trying a similar approach, it seems so, I'm just testing with all scenarios (in particular augmenting the DefaultResoures to see if it's inferring the appropriate Return Type). |
@pedrodurek Great 👍 I wouldn't be using or testing the type inference from an augmented |
Is the backend in the same repo? If so, it's possible. |
Hey @BenJenkinson, I had to change the approach because the generic TResult must be on the
When you have the chance, could you try with the fix here? pedrodurek@0fb1140 |
Hi @pedrodurek,
I don't think that would work easily for us, our translations can be dynamic.
That commit seems to no longer on a branch, so I've tested the file at pedrodurek@24b1ee4 which seems to be your latest commit. const a = t('exampleKey'); // ✅ string
const b: string[] = t('exampleKey'); // ✅ string[]
const c: string | null = t('exampleKey'); // ✅ string | null
const d: string | { foo: 'bar' } | undefined = t('exampleKey'); // ✅ string | { foo: 'bar' } | undefined
const e: string = t(['foo', 'bar']); // ✅ string
const f: string[] = t(['foo', 'bar']); // ✅ string[] That certainly builds now, and seems to be working OK. Thanks! 🥳 |
Hi @Him-2C, Would you check if your problem is also resolved by the new version 11.8.3? (and close this issue if it does) 🙂 |
@BenJenkinson @pedrodurek So now, all me it's work!. |
Works here too, thanks for all the good work guys! |
Works for me now as well. You folks rock! |
🐛 Bug Report
Has error on 11.8.1 or newer, but 11.7.3 it's work!
Your Environment
The text was updated successfully, but these errors were encountered: