Skip to content

Commit 2e97239

Browse files
authored
fix(use-i18n): correct formatList and Context type (#337)
1 parent 3a2204c commit 2e97239

File tree

3 files changed

+22
-27
lines changed

3 files changed

+22
-27
lines changed

packages/use-i18n/src/__tests__/usei18n.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,6 @@ describe('i18n hook', () => {
314314
}),
315315
).toEqual('$2.00')
316316

317-
expect(
318-
result.current.formatNumber(1.234, { style: 'unit', unit: 'kilobyte' }),
319-
).toEqual('1.234 kB')
320-
321317
act(() => {
322318
result.current.switchLocale('fr')
323319
})
@@ -552,6 +548,6 @@ describe('i18n hook', () => {
552548
expect(result.current.dateFnsLocale).toBe(undefined)
553549
await waitForNextUpdate()
554550

555-
expect(result.current.dateFnsLocale.code).toEqual('en-GB')
551+
expect(result.current.dateFnsLocale?.code).toEqual('en-GB')
556552
})
557553
})

packages/use-i18n/src/usei18n.tsx

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,32 +57,31 @@ const getCurrentLocale = ({
5757
interface Context {
5858
currentLocale: string
5959
dateFnsLocale?: Locale,
60-
datetime?: (date: Date | number, options?: Intl.DateTimeFormatOptions) => string,
61-
formatDate?: (value: Date | number | string, options: FormatDateOptions) => string,
62-
formatList?: (listFormat: [string | undefined], options?: Intl.ListFormatOptions) => string,
63-
formatNumber?: (numb: number, options?: Intl.NumberFormatOptions) => string,
64-
formatUnit?: (value: number, options: FormatUnitOptions) => string,
65-
loadTranslations?: (namespace: string, load?: LoadTranslationsFn) => Promise<string>,
66-
locales?: string[],
67-
namespaces?: string[],
68-
namespaceTranslation?: (namespace: string, t?: TranslateFn) => TranslateFn
69-
relativeTime?: (date: Date | number, options?: {
60+
datetime: (date: Date | number, options?: Intl.DateTimeFormatOptions) => string,
61+
formatDate: (value: Date | number | string, options: FormatDateOptions) => string,
62+
formatList: (listFormat: string[], options?: Intl.ListFormatOptions) => string,
63+
formatNumber: (numb: number, options?: Intl.NumberFormatOptions) => string,
64+
formatUnit: (value: number, options: FormatUnitOptions) => string,
65+
loadTranslations: (namespace: string, load?: LoadTranslationsFn) => Promise<string>,
66+
locales: string[],
67+
namespaces: string[],
68+
namespaceTranslation: (namespace: string, t?: TranslateFn) => TranslateFn
69+
relativeTime: (date: Date | number, options?: {
7070
includeSeconds?: boolean;
7171
addSuffix?: boolean;
7272
}) => string,
73-
relativeTimeStrict?: (date: Date | number, options?: {
73+
relativeTimeStrict: (date: Date | number, options?: {
7474
addSuffix?: boolean;
7575
unit?: 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year';
7676
roundingMethod?: 'floor' | 'ceil' | 'round';
7777
}) => string,
78-
setTranslations?: React.Dispatch<React.SetStateAction<TranslationsByLocales>>,
79-
switchLocale?: (locale: string) => void,
80-
t?: TranslateFn,
81-
translations?: TranslationsByLocales,
78+
setTranslations: React.Dispatch<React.SetStateAction<TranslationsByLocales>>,
79+
switchLocale: (locale: string) => void,
80+
t: TranslateFn,
81+
translations: TranslationsByLocales,
8282
}
8383

84-
// @ts-expect-error we force the context to undefined, should be corrected with default values
85-
const I18nContext = createContext<Context>(undefined)
84+
const I18nContext = createContext<Context | undefined>(undefined)
8685

8786
export const useI18n = (): Context => {
8887
const context = useContext(I18nContext)
@@ -214,7 +213,7 @@ const I18nContextProvider = ({
214213
)
215214

216215
const formatList = useCallback(
217-
(listFormat: [string | undefined], options?: Intl.ListFormatOptions) =>
216+
(listFormat: string[], options?: Intl.ListFormatOptions) =>
218217
formatters.getListFormat(currentLocale, options).format(listFormat),
219218
[currentLocale],
220219
)

types/intl.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
declare namespace Intl {
22
interface ListFormatOptions {
3-
localeMatcher: 'best fit' | 'lookup'
4-
type: 'conjunction' | 'disjunction' | 'unit'
5-
style: 'long' | 'short' | 'narrow'
3+
localeMatcher?: 'best fit' | 'lookup'
4+
type?: 'conjunction' | 'disjunction' | 'unit'
5+
style?: 'long' | 'short' | 'narrow'
66
}
77

88
interface ListFormat {
9-
format: (items: [string?]) => string;
9+
format: (items: string[]) => string;
1010
}
1111

1212
const ListFormat: {

0 commit comments

Comments
 (0)