-
-
Notifications
You must be signed in to change notification settings - Fork 673
Refactor generics usage #1180
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
Refactor generics usage #1180
Conversation
@rosskevin I saw that First of all, I think as expected the error from
It should be
But It still does not fix the error. And I actually don't know why |
Agreed @VincentLanglet - I tried a variety of things - none of which worked. The specific error from what you mentioned is:
I normally parameterize the interface and use in the method. I haven't tried that because I don't see how that would make a difference but perhaps I should. @tkow - thoughts since you introduced this originally? |
BTW - the diff is now cleaned up and much simpler. |
@rosskevin I get no error this way
|
Given that @VincentLanglet - I think we need to make a type-breaking change and just update the |
@rosskevin I Agree |
TranslateOptions -> TOptions WithT.t - removed definition and extracted to TFunction exists - stopped (mis)using TranslateFunction and defined separately
Ok, I think I have cleaned it up, though the generics test fails here:
Thoughts? Please check the updated description for a summary. I am tempted to comment out this test until @tkow has time to take a look. I think it is an important improvement that I think others may be running into. |
@rosskevin Indeed, this way we lose the ability to use Since the PR of tkow introduced a regression, I understand you comment out theses tests, in order to fix it. We will search how to have a better t function after. |
… it - breaks mock.test but works for typical usage
There is still a debate about the choice No breaking change, but not classic
Versus Breaking change, but classic
|
@VincentLanglet the goal has always been to support classic usage. I just added some to t.test to demonstrate expected/typical use: // various returns <string> is the default
i18next.t('friend');
i18next.t<object>('friend');
i18next.t<string[]>('friend');
i18next.t<object[]>('friend'); This should be the simplest use I think. |
The current push works for everything except the mock - back to square one but with an improved basis. I strangely have to duplicate |
@rosskevin When @tkow made his PR, it was intented to be used this way I think. But it's debatable
Anyway, the actual real problem is that we are not able to type correctly in order to having both generic type and the test for the mock working. We're doing something wrong... but hard to find what. |
…move TFunction duplication
Duplication removed, change init test to rely on inference instead of explicit types for callbacks, still have the mock issue. This works now without explicit types (unless you want the return type of object or string[]). For parameterized If I can get a good solution to the mock test, I think this is good to go. |
Added a workaround for the mock.test for the time being and filed an issue. |
@jamuhl I need a release for this. This is a breaking typescript change (see description) - it primarily fixes a regression introduced but to do so had to change order of parameterized args. The best way to not silently cause a lot of confusion for TS users is to break it in very obvious way - change the name of the interfaces that were affected. A major bump would make sense for TS users, but not for js users - your call on versioning. If you aren't worried, I would major bump it. I will have a follow-up PR/release to react-i18next related to this. |
@rosskevin published in i18next@14.0.0 |
* TS generics fixes related to i18next/1180 i18next/i18next#1180 * Bump i18next dependency to needed 14.0.0
I just updated my dependencies and apperently I am not able to do this anymore: import { WithNamespaces, withNamespaces } from "react-i18next";
export interface IDurationProps extends WithNamespaces {
/* other props */
}
const Duration: React.FC<IDurationProps> = ({ t }) => {
return <p>{t("my-key")}</p>
}
export default withNamespaces()(Duration); My test: describe("Duration", () => {
const props: IDurationProps = {
duration: 1800000,
size: "medium",
i18n: null,
tReady: true,
t: (key) => "h [hrs] mm [min]" // <-- Error: Compile Error!
};
/* omitted */
|
BREAKING type change for those using generics and importing/reusing types. Typical usage in TS without importing
TranslateFunction
or generics should remain unaffected.This fixes a regression introduced by #1163 and related PRs, but in turn disables use of generics in
i18next.t<>
pattern of usage. - see #1180 (comment)As I searched for a solution to this mock test, after a suggestion below by @VincentLanglet I noticed we would be moving towards duplication. As I attempted to solve this, I think (due to duplication), I believe I ran into #1175 (comment).
In order to solve this, since I needed to remove duplication and alter potential usage of parameterized arg order, the easiest way to signal this to TS users is to change the exported name so that they will intentionally break and need to review their usage (sorry everyone 👋 - seems like the best option)
TranslateFunction
->TFunction
TranslateOptions
->TOptions
WithT.t
- removed definition and extracted toTFunction
exists
- stopped (mis)usingTranslateFunction
and defined separatelyTFunction
args to make easier for typical use (result first)TFunction
to returnstring
by default. For arrays and objects, parameterize the call e.g.t<object>('foo')
Mock test initiated by i18next/react-i18next#684 @nikolay-borzov
The mock should have been simple - but it wasn't.