-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
feat(typings): support readonly namespaces in TFuncKey #1340
feat(typings): support readonly namespaces in TFuncKey #1340
Conversation
@adrai are you the ts guru ? 😄 The reason why I'm trying to have a config object is to reuse it in an hybrid app (nextjs)... so my both my ssr and client page share the same namespace config... It's just a detail and I can really live without. |
No, it's @pedrodurek 😉 |
Tried with no luck to find a better option than changing the TFuncKey, and do it in the useTranslation return but still no luck In the meantime, we can deal with this in the code as the i.e: export const nsPageConfig = ['common', 'home'] as const;
export const Page = () => {
const { t } = useTranslation(
// Calling slice here will remove the readonly constraint and
// so we're fine somehow
nsPageConfig.slice()
);
return (
<div>
// 2. Ok it works, the typecheck occurs
<span>{t('home:thekeyexists')}</span>
// 3. OK now it correctly complain cause other-context is not in nsPageConfig
<span>{t('other-context:thekeyexists')}</span>
</div>
);
} Update A little check in ast is present 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. |
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.
Looks great, thanks @belgattitude!
patch or minor? |
patch |
released with v11.11.2 |
Follow up of
While the initial P/R allows to readonly namespace array... the
t
function choose to fallback to all configured namespaces (ie: 'common', 'home', 'other-context')... defeating perfect typechecksWith this P/R it will work well.
But, I'd like to have a second reading on this... cause in my guts I feel that changing the signature of TFuncKey would see a theoretical drop of performance...
I need to check it out on a larger project or maybe a typescript guru would find a better way ?
An option that I'm investigating also is to remove readonly from useTranslation return, I don't know how to it... Something in the lines of
Checklist
npm run test