Skip to content

Commit

Permalink
fix: Make all keys of Formats type optional for easier usage (#1367)
Browse files Browse the repository at this point in the history
```diff
import {Formats} from 'next-intl';

- Partial<Formats>
+ Formats
```
  • Loading branch information
amannn authored Sep 24, 2024
1 parent 12fd5c5 commit a7cbd9b
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 29 deletions.
6 changes: 3 additions & 3 deletions packages/next-intl/src/react-server/getTranslator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function getTranslatorImpl<
>(
key: TargetKey,
values?: TranslationValues,
formats?: Partial<Formats>
formats?: Formats
): string;

// `rich`
Expand All @@ -58,7 +58,7 @@ function getTranslatorImpl<
>(
key: TargetKey,
values?: RichTranslationValues,
formats?: Partial<Formats>
formats?: Formats
): string | ReactElement | ReactNodeArray;

// `markup`
Expand All @@ -78,7 +78,7 @@ function getTranslatorImpl<
>(
key: TargetKey,
values?: MarkupTranslationValues,
formats?: Partial<Formats>
formats?: Formats
): string;

// `raw`
Expand Down
12 changes: 6 additions & 6 deletions packages/next-intl/src/server/react-server/getTranslations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Promise<{
>(
key: [TargetKey] extends [never] ? string : TargetKey,
values?: TranslationValues,
formats?: Partial<Formats>
formats?: Formats
): string;

// `rich`
Expand All @@ -63,7 +63,7 @@ Promise<{
>(
key: [TargetKey] extends [never] ? string : TargetKey,
values?: RichTranslationValues,
formats?: Partial<Formats>
formats?: Formats
): string | ReactElement | ReactNodeArray;

// `markup`
Expand All @@ -83,7 +83,7 @@ Promise<{
>(
key: [TargetKey] extends [never] ? string : TargetKey,
values?: MarkupTranslationValues,
formats?: Partial<Formats>
formats?: Formats
): string;

// `raw`
Expand Down Expand Up @@ -132,7 +132,7 @@ Promise<{
>(
key: TargetKey,
values?: TranslationValues,
formats?: Partial<Formats>
formats?: Formats
): string;

// `rich`
Expand All @@ -152,7 +152,7 @@ Promise<{
>(
key: TargetKey,
values?: RichTranslationValues,
formats?: Partial<Formats>
formats?: Formats
): string | ReactElement | ReactNodeArray;

// `markup`
Expand All @@ -172,7 +172,7 @@ Promise<{
>(
key: TargetKey,
values?: MarkupTranslationValues,
formats?: Partial<Formats>
formats?: Formats
): string;

// `raw`
Expand Down
6 changes: 3 additions & 3 deletions packages/use-intl/src/core/Formats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import DateTimeFormatOptions from './DateTimeFormatOptions';
import NumberFormatOptions from './NumberFormatOptions';

type Formats = {
number: Record<string, NumberFormatOptions>;
dateTime: Record<string, DateTimeFormatOptions>;
list: Record<string, Intl.ListFormatOptions>;
number?: Record<string, NumberFormatOptions>;
dateTime?: Record<string, DateTimeFormatOptions>;
list?: Record<string, Intl.ListFormatOptions>;
};

export default Formats;
2 changes: 1 addition & 1 deletion packages/use-intl/src/core/IntlConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type IntlConfig<Messages = AbstractIntlMessages> = {
locale: string;
/** Global formats can be provided to achieve consistent
* formatting across components. */
formats?: Partial<Formats>;
formats?: Formats;
/** A time zone as defined in [the tz database](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) which will be applied when formatting dates and times. If this is absent, the user time zone will be used. You can override this by supplying an explicit time zone to `formatDateTime`. */
timeZone?: TimeZone;
/** This callback will be invoked when an error is encountered during
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function setTimeZoneInFormats(
* to convert the format before `intl-messageformat` can be used.
*/
export default function convertFormatsToIntlMessageFormat(
formats: Partial<Formats>,
formats: Formats,
timeZone?: TimeZone
): Partial<IntlFormats> {
const formatsWithTimeZone = timeZone
Expand Down
4 changes: 2 additions & 2 deletions packages/use-intl/src/core/createBaseTranslator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ function createBaseTranslatorImpl<
/** Key value pairs for values to interpolate into the message. */
values?: RichTranslationValues,
/** Provide custom formats for numbers, dates and times. */
formats?: Partial<Formats>
formats?: Formats
): string | ReactElement | ReactNodeArray {
if (messagesOrError instanceof IntlError) {
// We have already warned about this during render
Expand Down Expand Up @@ -360,7 +360,7 @@ function createBaseTranslatorImpl<
/** Key value pairs for values to interpolate into the message. */
values?: TranslationValues,
/** Provide custom formats for numbers, dates and times. */
formats?: Partial<Formats>
formats?: Formats
): string {
const result = translateBaseFn(key, values, formats);

Expand Down
2 changes: 1 addition & 1 deletion packages/use-intl/src/core/createFormatter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type Props = {
locale: string;
timeZone?: TimeZone;
onError?(error: IntlError): void;
formats?: Partial<Formats>;
formats?: Formats;
now?: Date;
/** @private */
_formatters?: Formatters;
Expand Down
6 changes: 3 additions & 3 deletions packages/use-intl/src/core/createTranslator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default function createTranslator<
>(
key: TargetKey,
values?: TranslationValues,
formats?: Partial<Formats>
formats?: Formats
): string;

// `rich`
Expand All @@ -85,7 +85,7 @@ export default function createTranslator<
>(
key: TargetKey,
values?: RichTranslationValues,
formats?: Partial<Formats>
formats?: Formats
): string | ReactElement | ReactNodeArray;

// `markup`
Expand All @@ -105,7 +105,7 @@ export default function createTranslator<
>(
key: TargetKey,
values?: MarkupTranslationValues,
formats?: Partial<Formats>
formats?: Formats
): string;

// `raw`
Expand Down
12 changes: 6 additions & 6 deletions packages/use-intl/src/react/useTranslations.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ vi.mock('intl-messageformat', async (importOriginal) => {
function renderMessage(
message: string,
values?: TranslationValues,
formats?: Partial<Formats>,
formats?: Formats,
providerProps?: Partial<ComponentProps<typeof IntlProvider>>
) {
function Component() {
Expand Down Expand Up @@ -404,7 +404,7 @@ describe('t.rich', () => {
function renderRichTextMessage(
message: string,
values?: RichTranslationValues,
formats?: Partial<Formats>
formats?: Formats
) {
function Component() {
const t = useTranslations();
Expand Down Expand Up @@ -884,8 +884,8 @@ describe('error handling', () => {
describe('global formats', () => {
function renderDate(
message: string,
globalFormats?: Partial<Formats>,
overrideFormats?: Partial<Formats>
globalFormats?: Formats,
overrideFormats?: Formats
) {
function Component() {
const t = useTranslations();
Expand Down Expand Up @@ -952,7 +952,7 @@ describe('default translation values', () => {
function renderRichTextMessageWithDefault(
message: string,
values?: RichTranslationValues,
formats?: Partial<Formats>
formats?: Formats
) {
function Component() {
const t = useTranslations();
Expand All @@ -977,7 +977,7 @@ describe('default translation values', () => {
function renderMessageWithDefault(
message: string,
values?: TranslationValues,
formats?: Partial<Formats>
formats?: Formats
) {
function Component() {
const t = useTranslations();
Expand Down
6 changes: 3 additions & 3 deletions packages/use-intl/src/react/useTranslations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default function useTranslations<
>(
key: TargetKey,
values?: TranslationValues,
formats?: Partial<Formats>
formats?: Formats
): string;

// `rich`
Expand All @@ -65,7 +65,7 @@ export default function useTranslations<
>(
key: TargetKey,
values?: RichTranslationValues,
formats?: Partial<Formats>
formats?: Formats
): string | ReactElement | ReactNodeArray;

// `markup`
Expand All @@ -85,7 +85,7 @@ export default function useTranslations<
>(
key: TargetKey,
values?: MarkupTranslationValues,
formats?: Partial<Formats>
formats?: Formats
): string;

// `raw`
Expand Down

0 comments on commit a7cbd9b

Please sign in to comment.