Skip to content

Commit

Permalink
fix: Make the type fallback to string
Browse files Browse the repository at this point in the history
  • Loading branch information
dBianchii committed Sep 18, 2024
1 parent 7ab9a72 commit 4c12355
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
7 changes: 3 additions & 4 deletions examples/example-app-router/src/i18n/request.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {notFound} from 'next/navigation';
import {Formats} from 'next-intl';
import {getRequestConfig} from 'next-intl/server';
import {routing} from './routing';
import { Formats } from 'next-intl';

export const formats = {
dateTime: {
Expand All @@ -22,8 +22,7 @@ export const formats = {
type: 'conjunction'
}
}
} as const satisfies Partial<Formats>

} as const satisfies Partial<Formats>;

export default getRequestConfig(async ({locale}) => {
// Validate that the incoming `locale` parameter is valid
Expand All @@ -37,5 +36,5 @@ export default getRequestConfig(async ({locale}) => {
: import(`../../messages/${locale}.json`))
).default,
formats
}
};
});
24 changes: 20 additions & 4 deletions packages/use-intl/src/core/createFormatter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ export default function createFormatter({
value: Date | number,
/** If a time zone is supplied, the `value` is converted to that time zone.
* Otherwise the user time zone will be used. */
formatOrOptions?: keyof IntlFormats['dateTime'] | DateTimeFormatOptions
formatOrOptions?:
| (keyof IntlFormats['dateTime'] extends string
? IntlFormats['dateTime']
: string)
| DateTimeFormatOptions
) {
return getFormattedValue(
formatOrOptions,
Expand All @@ -183,7 +187,11 @@ export default function createFormatter({
end: Date | number,
/** If a time zone is supplied, the values are converted to that time zone.
* Otherwise the user time zone will be used. */
formatOrOptions?: keyof IntlFormats['dateTime'] | DateTimeFormatOptions
formatOrOptions?:
| (keyof IntlFormats['dateTime'] extends string
? IntlFormats['dateTime']
: string)
| DateTimeFormatOptions
) {
return getFormattedValue(
formatOrOptions,
Expand All @@ -200,7 +208,11 @@ export default function createFormatter({

function number(
value: number | bigint,
formatOrOptions?: keyof IntlFormats['number'] | NumberFormatOptions
formatOrOptions?:
| (keyof IntlFormats['number'] extends string
? IntlFormats['number']
: string)
| NumberFormatOptions
) {
return getFormattedValue(
formatOrOptions,
Expand Down Expand Up @@ -284,7 +296,11 @@ export default function createFormatter({
type FormattableListValue = string | ReactElement;
function list<Value extends FormattableListValue>(
value: Iterable<Value>,
formatOrOptions?: IntlFormats['list'] | Intl.ListFormatOptions
formatOrOptions?:
| (keyof IntlFormats['list'] extends string
? IntlFormats['list']
: string)
| Intl.ListFormatOptions
): Value extends string ? string : Iterable<ReactElement> {
const serializedValue: Array<string> = [];
const richValues = new Map<string, Value>();
Expand Down
6 changes: 0 additions & 6 deletions packages/use-intl/src/react/useFormatter.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ describe('dateTime', () => {
it('can use a global date format', () => {
function Component() {
const format = useFormatter();
// @ts-expect-error
return <>{format.dateTime(mockDate, 'onlyYear')}</>;
}

Expand All @@ -83,7 +82,6 @@ describe('dateTime', () => {
it('can use a global time format', () => {
function Component() {
const format = useFormatter();
// @ts-expect-error
return <>{format.dateTime(mockDate, 'onlyHours')}</>;
}

Expand Down Expand Up @@ -193,7 +191,6 @@ describe('dateTime', () => {

function Component() {
const format = useFormatter();
// @ts-expect-error
return <>{format.dateTime(mockDate, 'onlyYear')}</>;
}

Expand Down Expand Up @@ -221,7 +218,6 @@ describe('dateTime', () => {

function Component() {
const format = useFormatter();
// @ts-expect-error
return <>{format.dateTime(mockDate, 'medium')}</>;
}

Expand Down Expand Up @@ -319,7 +315,6 @@ describe('number', () => {
it('can use a global format', () => {
function Component() {
const format = useFormatter();
// @ts-expect-error
return <>{format.number(10000, 'noGrouping')}</>;
}

Expand Down Expand Up @@ -375,7 +370,6 @@ describe('number', () => {

function Component() {
const format = useFormatter();
// @ts-expect-error
return <>{format.number(mockNumber, 'missing')}</>;
}

Expand Down
6 changes: 3 additions & 3 deletions packages/use-intl/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ declare interface IntlMessages extends Record<string, any> {}
// This type is intended to be overridden
// by the consumer for optional type safety of formats
declare interface IntlFormats {
dateTime: {};
number: {};
list: {};
dateTime: any;
number: any;
list: any;
}

// Temporarly copied here until the "es2020.intl" lib is published.
Expand Down

0 comments on commit 4c12355

Please sign in to comment.