@@ -3,6 +3,22 @@ import IntlTranslationFormat from 'intl-messageformat'
3
3
4
4
// Deeply inspired by https://github.com/formatjs/formatjs/blob/7406e526a9c5666cee22cc2316dad1fa1d88697c/packages/intl-messageformat/src/core.ts
5
5
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
+
6
22
interface BaseFormatters {
7
23
getNumberFormat (
8
24
...args : ConstructorParameters < typeof Intl . NumberFormat >
@@ -14,8 +30,8 @@ interface BaseFormatters {
14
30
...args : ConstructorParameters < typeof Intl . PluralRules >
15
31
) : Intl . PluralRules
16
32
getListFormat (
17
- ...args : ConstructorParameters < typeof Intl . ListFormat >
18
- ) : Intl . ListFormat
33
+ ...args : ConstructorParameters < typeof IntlListFormat >
34
+ ) : IntlListFormat
19
35
}
20
36
21
37
function createFastMemoizeCache < V > ( ) : Cache < string , V > {
@@ -40,8 +56,10 @@ const baseFormatters: BaseFormatters = {
40
56
cache : createFastMemoizeCache < Intl . DateTimeFormat > ( ) ,
41
57
strategy : strategies . variadic ,
42
58
} ) ,
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 > ( ) ,
45
63
strategy : strategies . variadic ,
46
64
} ) ,
47
65
getNumberFormat : memoize ( ( ...args ) => new Intl . NumberFormat ( ...args ) , {
0 commit comments