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
Restrict interpolation types based on format; Include typing for formatParams #1999
Conversation
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
@pedrodurek and also here? |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I'm not sure we should enforce the format typings... what if someone uses the old formatting or custom formats? |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
@marcalexiei what is your opinion on this? |
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.
I tried to port the on master
branch.
(This PR is very old: InterpolationMap
is inside t.d.ts
files, here is inside index.d.ts
😅)
Right now tests are failing:
The possible breaking change is that someone could have already declared formatter with these names but with different parameters like @adrai pointed out.
Simple solution
This computation should be performed only when _JSONFormat
is set to v4
like in PluralSuffix
.
Lines 35 to 37 in 2f3384b
type PluralSuffix = _JSONFormat extends 'v4' | |
? 'zero' | 'one' | 'two' | 'few' | 'many' | 'other' | |
: number | 'plural'; |
However if someone is using a custom parser with JSONFormat
set to v4
the issue will remain, so the only workaround I could think of is to add a new boolean flag under CustomTypeOptions
to enable this kind of behaviour.
I personally don't find any of this two solutions quite appealing because I consider them a workaround.
Complex solution
The best, but more difficult, solution is to add a way to define formatters inside CustomTypeOptions
so every user is able to customise them and they are inferred by the TFunction
type when the key has them.
Basically something like what is already in place with Resources
.
Unfortunately that is a lot of type gymnastic.
Besides that I think that more tests should be added to test the behaviour of TFunction
when providing a formatter using Intl
API.
ok, will close this for now... if someone has time and motivation to take this over feel free to reopen or create a new PR |
Currently, it's possible to pass any variable as an interpolation, which isn't great since it can be error prone. Take the following as an example:
The above is not completely addressed in this PR. Instead, I'm first fixing a subset of this problem, which is formatted interpolations. Since the built-in interpolations must conform to the
Intl
formatters that they are passed to, this PR makes it so that any interpolation that is being formatted is restriced to the type that the correspondingIntl
formatter expects. This PR also adds typing forformatParams
.Checklist
npm run test