-
-
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
Undefined as a return type for t from useTranslation is not allowed as React Element #794
Comments
|
good question - in my world i don't get an undefined back from t - all i get is an empty string when calling with empty string as key...so would be nice to reproduce this...but for that i would need more informations but guess this is more a warning of typescript (than the javascript truth of code) |
This is a typescript issue, but ts should strictly reflect the runtime, that's why I ask. I thought an init option made undefined possible. |
returnNull -> allows null as value in JSON but fallback always is key if remembering right - at least until overriding this using parseMissingHandler or postProcessor... |
Thank you for your quick replies :). A components return value should never be undefined. It is not supported and your app will crash.
I think that t behaves correctly, but the type definitions in typescript for the return value of t are wrong. There should be no undefined, because that would be illegal. |
@danielbischoff please provide your tsconfig and error (second request). I already use strict and have no problem. |
Okay so I upgraded again, and now these errors are gone. I think sth. went wrong while upgrading. |
Explanation here: i18next/i18next#1221 (comment) |
I am very confused. Because I think the type is wrong. string | object | Array<string | object> | undefined = string, But React (jsx) can only handle the type So imo, the return type of t should only be string or null (or only string, if null is never returned) |
@danielbischoff you are mixing up runtime vs static typechecking. See the linked ts explanation above. Just so you are aware, we already test this so you should not see any ts error. https://github.com/i18next/react-i18next/blob/master/test/typescript/useTranslation.test.tsx |
Okay, thank you @rosskevin ! |
First off, sorry to raise up a question in such an old issue. However, I've stumbled over this issue (in combination with [1]) and I suspect the nullish inference is just for convenience. Right off the bat, I don't see any obvious place where translate would ever return undefined (looking at [2]) which leads me to think that the type signature is incorrect as it doesn't reflect the actual implementation. This could potentially result in undefined behavior and bugs, provided that the consumer expects these strict types. In fact, similarly, I don't see any way either to distinguish string outputs from object/array outputs (as is tested here: [3]). You would need runtime information to determine if it's the prior or the latter. While I'm aware the
As a quick fix to my actual problem I would have to augment
Now, I just hope that no colleague of mine modifies the translations into an object based translation, resulting in edit: I guess you already support Here's an inferrable version of import { TOptions, TFunctionKeys, TFunctionResult, StringMap } from 'i18next';
declare module 'i18next' {
type ReturnObjectsOutput = object | Array<string | object>;
type ReturnObjectsConfig = { returnObjects: true };
type NewTFunctionResult = Exclude<TFunctionResult, ReturnObjectsOutput>;
export interface TFunction {
// w/ returnObjects: true
<
TResult extends ReturnObjectsOutput,
TKeys extends TFunctionKeys = string,
TInterpolationMap extends object = StringMap
>(
key: TKeys | TKeys[],
options: (TOptions<TInterpolationMap> & ReturnObjectsConfig) | string,
): TResult;
// Two arguments
<
TResult extends NonNullable<NewTFunctionResult> = string,
TKeys extends TFunctionKeys = string,
TInterpolationMap extends object = StringMap
>(
key: TKeys | TKeys[],
options?: TOptions<TInterpolationMap> | string,
): TResult;
<
TResult extends NewTFunctionResult = string,
TKeys extends TFunctionKeys = string,
TInterpolationMap extends object = StringMap
>(
key: TKeys | TKeys[],
options?: TOptions<TInterpolationMap> | string,
): TResult;
// Three arguments
<
TResult extends NonNullable<NewTFunctionResult> = string,
TKeys extends TFunctionKeys = string,
TInterpolationMap extends object = StringMap
>(
key: TKeys | TKeys[],
defaultValue?: string,
options?: TOptions<TInterpolationMap> | string,
): TResult;
<
TResult extends NewTFunctionResult = string,
TKeys extends TFunctionKeys = string,
TInterpolationMap extends object = StringMap
>(
key: TKeys | TKeys[],
defaultValue?: string,
options?: TOptions<TInterpolationMap> | string,
): TResult;
}
} |
this still doesn't work for me and I don't understand, what's the fix/solution? "i18next": "^21.4.0", |
I'm also running into this problem. The workaround (if it can be called a workaround) is |
@enoh-barbu and @tonghe7 this issue is old and there are so many changes that the content is likely not relevant. If you want to know how to setup either typescript or the usages of it, just check this repo's tests and tsconfigs. |
I don't remember what I did, but now I don't have this issue anymore |
Describe the bug
When you use the t function returned from useTranslation() in jsx, react/typescript will complain that undefined is not a valid JSX element type.
That is, because TFunction returns TResult and TResult can be undefined
Occurs in react-i18next version
10.5.2
To Reproduce
Expected behaviour
Using t directly in JSX should work according to docs:
https://react.i18next.com/latest/usetranslation-hook
OS (please complete the following information):
The text was updated successfully, but these errors were encountered: