-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Migrate @wordpress/i18n
package to TypeScript and Add TextDomain Type Safety to createI18n
#70843
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
createI18n
createI18n
@wordpress/i18n
package to TypeScript and Add TextDomain Type Safety to createI18n
…sed on suggesstion
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
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.
Awesome!
…pe Safety to `createI18n` (WordPress#70843) Co-authored-by: USERSATOSHI <tusharbharti@git.wordpress.org> Co-authored-by: swissspidy <swissspidy@git.wordpress.org> Co-authored-by: im3dabasia <im3dabasia1@git.wordpress.org> Co-authored-by: manzoorwanijk <manzoorwanijk@git.wordpress.org> Co-authored-by: danieliser <danieliser@git.wordpress.org>
IMHO, the use of that |
Hi @manzoorwanijk, during the working of transitioning from sprintf being a strictly typed function requires string literals to define the typings for the arguments provided to it based on the string literal in format. PR link: #70434 All translatable functions like the solution for this was export type TranslatableText< T extends string > = string & {
/**
* DO NOT USE! This property _does not exist_.
* @private
*/
readonly __translatableText: T;
}; a generic string with a readonly private property that is only being used for sprintf to generate args types. |
I understand the type-safety, but it doesn't represent the runtime behavior of those i18n functions whose return can be any translated text and not the string literal we set the type to. |
Hmm, I believe, @swissspidy and @aduth can provide their insights on this as they would know better, what tradeoffs or how to proceed with strict typing for all translatable functions and sprintf and how they work together or how to tackle this situation. In the meantime, I can only provide workarounds for this, which would be using union type to make TranslatableText know which messages would be in reassigns or |
@manzoorwanijk Also wanted to mention that in the #71104 mentioning #70434 would make more sense as TranslatableText being introduced in that has discussions on why it was needed. this might be useful for users who came through that issue needing a bit of background info. As this PR only introduced conversion from JSDocs to TypeScript and addition of strict type safety in text-domains for createI18n. |
Best to move this discussion to the newly opened issue for better visibility. IMHO the tradeoff with the type safety is worth it. Your example above can be simply fixed by making the variable a string. |
What?
Part of: #67691
Also Closes #68906
This PR adds support for TextDomain type-safety for i18n instance created from
createI18n
As well as migrate the@wordpress/i18n
package to TypeScript.Why?
This PR introduces type safety to
@wordpress/i18n
package as well as adds textDomain type checking.How?
By porting the code to TypeScript
Testing Instructions
Unit Tests for Type Checking should suffice.