-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Be explict about some i18n oddness around undefined keys, and TextField's value #2598
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
Conversation
f28c066
to
e013624
Compare
// Use a typeof check here as Typescript mostly protects us from non-stringy | ||
// values but overzealous usage of `any` in consuming apps means people have | ||
// been known to pass a number in, so make it clear that doesn't work. | ||
const normalizedValue = typeof value === 'string' ? value : ''; |
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 like this approach.
src/utilities/i18n/I18n.ts
Outdated
// for now as JS can handle it ok. We should work out how to be | ||
// stricter, or deliberatly allow undefined as a value | ||
return replacements[replacement] as string; | ||
return replacements[replacement].toString(); |
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.
👍
`No replacement found for key 'string'. The following replacements were passed: 'notString'`, | ||
`Error in translation for key 'key'. No replacement found for key 'string'. The following replacements were passed: '{"notString":"bar"}'`, | ||
); | ||
|
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.
Not 100% necessary but it might be best to split these 2 expect statements into 2 tests?
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.
Thanks for taking care of this @BPScott
e013624
to
d263c54
Compare
Improve the error message, and have the error trigger if you have `{foo: undefined}`
This ensures that normalizedValue is always a string - previously it could be a number if you forced it through with `any` and that caused problems further down the line
d263c54
to
29e86d1
Compare
A bit or rebasing to split these changes into isolated commits |
WHY are these changes introduced?
Following up #2579.
WHAT is this pull request doing?
Make passing
{foo: undefined}
into translations replacements a runtime error, in addition to a TS type error for those fun cases where people power through mistyping things usingany
(looking at you enzyme's trigger function that isn't type-safe).Make TextField's
normalizedValue
be an empty string if you pass anything other than a string into it. Once again this is limited to be a string only by typescript but people using those not-type-safe functions can pass in a number and not be warned. Having normalizedValue be a number mucks up a bunch of things so catch that at runtime too.Make TextField a little lazier by only working out character count stuff if we're actually about to display it.
How to 🎩
tests pass