Skip to content
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

fix: #112 - default NestedKey to never #115

Merged
merged 3 commits into from
May 30, 2022
Merged

Conversation

kelsny
Copy link
Contributor

@kelsny kelsny commented May 27, 2022

Fixes #112

Modified files:

  • packages/use-intl/src/useTranslations.tsx

It's just my comment with the ideas applied:

And in the conditional, I just check if NestedKey is never: [NestedKey] extends [never] ? ... : .... It seems to work fine.

You can find the fix here

@vercel
Copy link

vercel bot commented May 27, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
next-intl ✅ Ready (Inspect) Visit Preview May 27, 2022 at 9:05PM (UTC)

Copy link
Owner

@amannn amannn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @kelsny, this is incredibly nice of you to directly submit a PR! Thank you so much for your help!

NamespaceKeys<IntlMessages, NestedKeyOf<IntlMessages>> extends NestedKey
? '!'
: `!.${NestedKey}`
[NestedKey] extends [never] ? '!' : `!.${NestedKey}`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From your issue comment:

When using generics as types for optional parameters, and the parameter is not given, the generic is defaulted to its constraint.
So I solved this (in a terrible way I did not test) by testing if the constraint extends NestedKey . Obviously, now that I think about it, this will break for objects with only one top-level property...
I also just learnt this very recently, so now I can solve this problem. I've given NestedKey a default "value" (it's actually a type, of course, but at the type-level I refer to types as "values") of never.
And in the conditional, I just check if NestedKey is never: [NestedKey] extends [never] ? ... : .... It seems to work fine.

I think I understand. Ultimately we want to check if namespace was provided at the type-level and never would mean that it wasn't. Makes sense to me! It's still a bit strange to me though why have to wrap the types into arrays for comparing them with extends.

But it's really great to see that this works! 👏

@amannn
Copy link
Owner

amannn commented May 30, 2022

I tested your fix in a larger codebase where I'm using next-intl and it seems to work flawlessly. Thank you very much for your help @kelsny!

@amannn amannn merged commit cf0b83e into amannn:main May 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeScript validation is broken when a single namespace is available in the messages
2 participants