-
Notifications
You must be signed in to change notification settings - Fork 2k
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
types: Uses textOnly
for translations
#59035
Conversation
Link to Calypso live: https://calypso.live?image=registry.a8c.com/calypso/app:build-22308 |
Here is how your PR affects size of JS and CSS bundles shipped to the user's browser: Sections (~24 bytes removed 📉 [gzipped])
Sections contain code specific for a given set of routes. Is downloaded and parsed only when a particular route is navigated to. Legend What is parsed and gzip size?Parsed Size: Uncompressed size of the JS and CSS files. This much code needs to be parsed and stored in memory. Generated by performance advisor bot at iscalypsofastyet.com. |
I'm not convinced this is the right solution:
Looking at the implementation of That will remove the burden from consumers to check for |
textOnly
for translationstextOnly
for translations
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.
Tested, LGTM.
The The former will transform the return value to a Despite not having any references to |
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 think this is not a good solution and have some ideas how to fix this problem much better. I'll describe them later, now I'm just giving 👎 to prevent merging this PR too soon.
client/lib/validation/file-path.ts
Outdated
@@ -13,18 +13,18 @@ const validateFilePath: validator< string > = ( | |||
} | |||
if ( filePathBackSlashRegExp.test( pathToValidate ) ) { | |||
return { | |||
message: translate( 'Use forward slashes, "/", in path.' ).toString(), | |||
message: translate( 'Use forward slashes, "/", in path.', { textOnly: true } ), |
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.
In this file, the translated strings should be returned as ReactNode
s, without textOnly
. They eventually end up as <FormInputValidation text>
props, rendered as HTML markup.
It's the ValidationError
type that should be fixed. message
is not a string
, but a ReactNode
. That basically means that the message
can use HTML formatting, e.g.:
Use forward slashes, <code>/</code>, in path.
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.
Fixed
).toString(), | ||
'FTP (File Transfer Protocol): the original standard for transferring files between servers.', | ||
{ textOnly: true } | ||
), |
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.
Here the items
should be ReactNode
s (i.e., HTML text) too: they are rendered as
<ul>{ items.map( text => <li>{ text }</li> ) }</ul>
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.
Fixed
As @yuliyan writes,
[*] Actually TIL that the two translate hooks in Calypso do an additional trick that would render <img alt={ translate( '{{b}}Hello{{/b}}', { components: { b: <b/> } } ) } /> would really render Now, could there be a better return type than type ReactChild = ReactElement | ReactText; and type ReactNode = ReactChild | ReactFragment | ReactPortal | boolean | null | undefined Here I tried to change It will also fix situations where a MyButton.propTypes = { text: PropTypes.node.isRequired };
const text: ReactNode = 'hello';
<MyButton text={ text } />;
// bang, ReactNode can be undefined which conflicts with the `.isRequired` bit or type Props = { text? ReactNode };
type ReqProps = Required<Props>;
const text: ReactNode = 'hello';
props: ReqProps = { text };
// bang, a TS error again |
3c38cd7
to
3011966
Compare
Background
We have a few TS files that do
translate(...).toString()
. The result oftranslate()
was declared asReact.ReactNode
in #58938. This type includesundefined | null
, therefore calling.toString()
in a value that is potentiallyundefined
ornull
is not safe.Changes proposed in this Pull Request
#58938 added the option
{textOnly:true}
to force the return type to bestring
, making calls to.toString()
valid. But of course, in that case, there is no point in calling.toString()
anymore, as the result oftranslate()
is already astring
.This should fix all these type errors: