@@ -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+
622interface 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
2137function 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 ) , {
0 commit comments