Skip to content

Instantly share code, notes, and snippets.

@cpv123
Last active February 8, 2023 15:21
Show Gist options
  • Save cpv123/afccb1a87d235b9300547f00180c3b99 to your computer and use it in GitHub Desktop.
Save cpv123/afccb1a87d235b9300547f00180c3b99 to your computer and use it in GitHub Desktop.
{
"helloWorld": "Hello world!",
"goodbye": "Goodbye
}
import type { ReactNode } from "react";
import type { IntlFormatters } from "react-intl";
import { useIntl as useReactIntl } from "react-intl";
import enMessages from "./en.json";
// Our new union type of all available message IDs.
type IntlMessageKeys = keyof typeof enMessages;
// The arguments to the original formatMessage function.
type FormatMessageArgs = Parameters<IntlFormatters["formatMessage"]>;
export function useIntl() {
// Pull out the original formatMessage function.
const { formatMessage, ...rest } = useReactIntl();
// Re-write the formatMessage function but with an enhanced id type.
const typedFormatMessage = (
descriptor: FormatMessageArgs[0] & {
id?: IntlMessageKeys; // <--- override the id type
},
values?: FormatMessageArgs[1],
options?: FormatMessageArgs[2]
) => {
return formatMessage(descriptor, values, options);
};
return {
...rest,
formatMessage: typedFormatMessage,
};
}
import type { ReactNode } from "react";
import type { Props as ReactIntlFormattedMessageProps } from "react-intl/src/components/message";
import { FormattedMessage as ReactIntlFormattedMessage } from "react-intl";
import enMessages from "./en.json";
// Our new union type of all available message IDs.
type IntlMessageKeys = keyof typeof enMessages;
// Extend the original FormattedMessage props.
type FormattedMessageProps = ReactIntlFormattedMessageProps<
Record<string, ReactNode>
> & {
id?: IntlMessageKeys;
};
export function FormattedMessage({ id, ...rest }: FormattedMessageProps) {
return <ReactIntlFormattedMessage id={id} {...rest} />;
}
import type { ReactNode } from "react";
import type { IntlFormatters } from "react-intl";
import type { Props as ReactIntlFormattedMessageProps } from "react-intl/src/components/message";
import {
FormattedMessage as ReactIntlFormattedMessage,
useIntl as useReactIntl,
} from "react-intl";
import enMessages from "./en.json";
// Our new union type of all available message IDs.
type IntlMessageKeys = keyof typeof enMessages;
// The arguments to the original formatMessage function.
type FormatMessageArgs = Parameters<IntlFormatters["formatMessage"]>;
// Extend the original FormattedMessage props.
type FormattedMessageProps = ReactIntlFormattedMessageProps<
Record<string, ReactNode>
> & {
id?: IntlMessageKeys;
};
export function FormattedMessage({ id, ...rest }: FormattedMessageProps) {
return <ReactIntlFormattedMessage id={id} {...rest} />;
}
export function useIntl() {
// Pull out the original formatMessage function.
const { formatMessage, ...rest } = useReactIntl();
// Re-write the formatMessage function but with a strongly-typed id.
const typedFormatMessage = (
descriptor: FormatMessageArgs[0] & {
id?: IntlMessageKeys;
},
values?: FormatMessageArgs[1],
options?: FormatMessageArgs[2]
) => {
return formatMessage(descriptor, values, options);
};
return {
...rest,
formatMessage: typedFormatMessage,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment