Skip to content

Commit c339adc

Browse files
authored
fix(use-i18n): do not use typescript global namespace (#343)
* fix(use-i18n): do not use typescript global namespace * fix: last one
1 parent 2d27bc5 commit c339adc

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

packages/use-i18n/src/formatters.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@ import IntlTranslationFormat from 'intl-messageformat'
33

44
// Deeply inspired by https://github.com/formatjs/formatjs/blob/7406e526a9c5666cee22cc2316dad1fa1d88697c/packages/intl-messageformat/src/core.ts
55

6+
// TS does not include non standard API
7+
// Intl.ListFormat in at TC39 stage 4 and is widely adopted in browsers
8+
// So we expose homegrown types
9+
// https://github.com/tc39/proposal-intl-list-format
10+
export interface IntlListFormatOptions {
11+
localeMatcher?: 'best fit' | 'lookup'
12+
type?: 'conjunction' | 'disjunction' | 'unit'
13+
style?: 'long' | 'short' | 'narrow'
14+
}
15+
16+
declare abstract class IntlListFormat {
17+
constructor(locales?: string | string[], options?: IntlListFormatOptions);
18+
19+
format: (items: string[]) => string;
20+
}
21+
622
interface BaseFormatters {
723
getNumberFormat(
824
...args: ConstructorParameters<typeof Intl.NumberFormat>
@@ -14,8 +30,8 @@ interface BaseFormatters {
1430
...args: ConstructorParameters<typeof Intl.PluralRules>
1531
): Intl.PluralRules
1632
getListFormat(
17-
...args: ConstructorParameters<typeof Intl.ListFormat>
18-
): Intl.ListFormat
33+
...args: ConstructorParameters<typeof IntlListFormat>
34+
): IntlListFormat
1935
}
2036

2137
function createFastMemoizeCache<V>(): Cache<string, V> {
@@ -40,8 +56,10 @@ const baseFormatters: BaseFormatters = {
4056
cache: createFastMemoizeCache<Intl.DateTimeFormat>(),
4157
strategy: strategies.variadic,
4258
}),
43-
getListFormat: memoize((...args) => new Intl.ListFormat(...args), {
44-
cache: createFastMemoizeCache<Intl.ListFormat>(),
59+
// @ts-expect-error we assume Intl.ListFormat exists in our current context
60+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
61+
getListFormat: memoize((...args) => new Intl.ListFormat(...args) as IntlListFormat, {
62+
cache: createFastMemoizeCache<IntlListFormat>(),
4563
strategy: strategies.variadic,
4664
}),
4765
getNumberFormat: memoize((...args) => new Intl.NumberFormat(...args), {

packages/use-i18n/src/usei18n.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ import ReactDOM from 'react-dom'
1414
import 'intl-pluralrules'
1515
import dateFormat, { FormatDateOptions } from './formatDate'
1616
import unitFormat, { FormatUnitOptions } from './formatUnit'
17-
import formatters from './formatters'
17+
import formatters, { IntlListFormatOptions } from './formatters'
1818

1919
const LOCALE_ITEM_STORAGE = 'locale'
2020

21+
type PrimitiveType = string | number | boolean | null | undefined | Date;
22+
2123
type Translations = Record<string, string> & { prefix?: string }
2224
type TranslationsByLocales = Record<string, Translations>
2325
type TranslateFn = (key: string, context?: Record<string, PrimitiveType>) => string
@@ -59,7 +61,7 @@ interface Context {
5961
dateFnsLocale?: Locale,
6062
datetime: (date: Date | number, options?: Intl.DateTimeFormatOptions) => string,
6163
formatDate: (value: Date | number | string, options: FormatDateOptions) => string,
62-
formatList: (listFormat: string[], options?: Intl.ListFormatOptions) => string,
64+
formatList: (listFormat: string[], options?: IntlListFormatOptions) => string,
6365
formatNumber: (numb: number, options?: Intl.NumberFormatOptions) => string,
6466
formatUnit: (value: number, options: FormatUnitOptions) => string,
6567
loadTranslations: (namespace: string, load?: LoadTranslationsFn) => Promise<string>,
@@ -213,7 +215,7 @@ const I18nContextProvider = ({
213215
)
214216

215217
const formatList = useCallback(
216-
(listFormat: string[], options?: Intl.ListFormatOptions) =>
218+
(listFormat: string[], options?: IntlListFormatOptions) =>
217219
formatters.getListFormat(currentLocale, options).format(listFormat),
218220
[currentLocale],
219221
)

types/global.d.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

types/intl.d.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)