-
-
Notifications
You must be signed in to change notification settings - Fork 236
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: Type-safe global formats
#1346
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@dBianchii is attempting to deploy a commit to the next-intl Team on Vercel. A member of the Team first needs to authorize it. |
Hi @ amann/maintainers You said in here that the package supports You also said:
I haven't found this logic in the code, and also in my tests when I do Could you explain a bit more of what you meant? EDIT: Looks like that the |
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.
Damn you're quick! 🔥🔥
I somehow left this feature on the backlog for a long time, but you made it happen just over night! 😄 Super cool, thanks a lot!
I've left a few comments inline, let me know what you think!
Btw. regarding the defaults across usage in messages / via format.*
: You're absolutely right, I've opened #1347 as a separate issue to discuss this. This is definitely out of scope for this PR and type-safety is only relevant for format.*
calls, therefore we should assume no defaults there.
There is one problem with this right now: I see that the global format api has been designed to be able to handle global formats for client and server differently/separately. (We must provide formats to both How can we resolve this problem? |
Yep, I agree! The discussion is related to the one about messages: Messages are defined globally, but it's the user's job to provide the relevant messages where relevant. This is to allow the user to optionally pass only certain messages to the client side to optimize bundle size, which is also the reason why the The current state of the library has the same mechanism for That being said, I think it's fine to model Hope that sounds reasonable to you! |
That's awesome to hear. Makes total sense to me. I'll continue with this decision in mind. Thanks! |
…anchii/next-intl into feat/typesafe-global-formats
I was thinking of moving this section to the bottom of the page, so it talks about both cases. But for now I just left it like that because I think that moving it all the way down would require creating another heading which I don't want to do |
- Configuration page: Smaller callout - TypeScript page: Less redundancy, shorter text, shared troubleshooting section, consistent ordering of sample/instructions in sections - Avoid `Partial<Format>`
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 had another final look and rephrased a few minor things along the way. I agree that the troubleshooting section can now be shared for both types.
I've also adapted the Formats
type in #1367 for easier consumption.
All good from my side now, waiting for CI to turn green! 🚀
I did another final test in I've experimented with a few options with this test suite: /* eslint-disable @typescript-eslint/no-unused-vars */
const formats = {
dateTime: {
medium: {
dateStyle: 'medium',
timeStyle: 'short',
hour12: false
}
}
};
type Formats = typeof formats;
interface IntlFormats extends Formats {}
type T1 = keyof IntlFormats['dateTime'] extends string
? keyof IntlFormats['dateTime'] | Intl.DateTimeFormatOptions
: string | Intl.DateTimeFormatOptions;
type T2 = keyof IntlFormats['dateTime'] | Intl.DateTimeFormatOptions;
type T3 =
| Extract<keyof IntlFormats['dateTime'], string>
| Intl.DateTimeFormatOptions;
// Type checking works, but no auto-completion
const t1: T1 = 'medium';
// Auto-completion, no string default
const t2: T2 = 'medium';
// Auto-completion and string default ✅
const t3: T3 = 'medium'; ChatGPT gave me the tip of using |
This PR adds a new configuration called
IntlFormats
, where users can link their global formatting to this interface, and allow for global formats typesafety to work across their app.Closes #1112