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

Typesafe Global formats #1340

Closed
dBianchii opened this issue Sep 17, 2024 · 2 comments
Closed

Typesafe Global formats #1340

dBianchii opened this issue Sep 17, 2024 · 2 comments
Labels
enhancement New feature or request unconfirmed Needs triage.

Comments

@dBianchii
Copy link
Contributor

dBianchii commented Sep 17, 2024

Is your feature request related to a problem? Please describe.

Global formats are awesome. It's great to help achieve consistency in my application. However, if I create these global settings for the formatter, they won't be typesafe
format.dateTime(new Date('2020-11-20T10:36:01.516Z'), 'short'); // => the second parameter requested is always of type 'string'.

Describe the solution you'd like

I'd like for the formatter to be of a narrower type, based on the template literal values of what I added in my config

Describe alternatives you've considered

In my global.d.ts file:

import { formats as myFormats } from "@kdx/locales";
declare module "use-intl" {
  function createFormatter({
    _formatters: formatters,
    formats,
    locale,
    now: globalNow,
    onError,
    timeZone: globalTimeZone,
  }: Props): {
    dateTime: (
      value: Date | number,
      formatOrOptions?: keyof typeof myFormats.dateTime | DateTimeFormatOptions,
    ) => string;
  };
}

my @kdx/locales file:

import type { Formats } from "use-intl";
export const formats = {
  dateTime: {
    extensive: {
      day: "2-digit",
      month: "long",
      year: "numeric",
      hour: "numeric",
      minute: "numeric",
    },
  },
} as const satisfies Partial<Formats> | undefined;

As you can see, this ALMOST works. I am extending/rewriting the return type of createFormatter from use-intl. In my code, now the dateFormat function is typesafe.
image

However, there is a big problem: since I completely redeclared the return type of createFormatter, now I all of the typesafety for all other methods are gone, and only this function is declared inside it 😛

@dBianchii dBianchii added enhancement New feature or request unconfirmed Needs triage. labels Sep 17, 2024
@amannn
Copy link
Owner

amannn commented Sep 17, 2024

I absolutely agree, would love to have this but haven't found the time yet to look into this. There's already an issue open at #1112, I'll close this in favor of the former one.

Let me know in case you'd be interested in helping out with this!

@amannn amannn closed this as completed Sep 17, 2024
@dBianchii
Copy link
Contributor Author

Sorry, I wasn't able to find this issue. I usually try to search for similar issues before posting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request unconfirmed Needs triage.
Projects
None yet
Development

No branches or pull requests

2 participants