@formatjs/intl 4.1.5 → 4.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -1,20 +1,306 @@
1
- import { type MessageDescriptor } from "./src/types.js";
2
- export * from "./src/types.js";
3
- export declare function defineMessages<
4
- K extends keyof any,
5
- T = MessageDescriptor,
6
- U extends Record<K, T> = Record<K, T>
7
- >(msgs: U): U;
8
- export declare function defineMessage<T>(msg: T): T;
9
- export { createIntlCache, filterProps, DEFAULT_INTL_CONFIG, createFormatters, getNamedFormat } from "./src/utils.js";
10
- export * from "./src/error.js";
11
- export { formatMessage } from "./src/message.js";
12
- export type { FormatMessageFn } from "./src/message.js";
13
- export { formatDate, formatDateToParts, formatTime, formatTimeToParts } from "./src/dateTime.js";
14
- export { formatDisplayName } from "./src/displayName.js";
15
- export { formatList } from "./src/list.js";
16
- export { formatPlural } from "./src/plural.js";
17
- export { formatRelativeTime } from "./src/relativeTime.js";
18
- export { formatNumber, formatNumberToParts } from "./src/number.js";
19
- export { createIntl } from "./src/create-intl.js";
20
- export type { CreateIntlFn } from "./src/create-intl.js";
1
+ import { FormatError, FormatXMLElementFn, Formats, Formatters as Formatters$1, IntlMessageFormat, Options, PrimitiveType } from "intl-messageformat";
2
+ import { MessageFormatElement } from "@formatjs/icu-messageformat-parser";
3
+
4
+ //#region packages/ecma402-abstract/types/number.d.ts
5
+ type NumberFormatNotation = "standard" | "scientific" | "engineering" | "compact";
6
+ type RoundingPriorityType = "auto" | "morePrecision" | "lessPrecision";
7
+ type RoundingModeType = "ceil" | "floor" | "expand" | "trunc" | "halfCeil" | "halfFloor" | "halfExpand" | "halfTrunc" | "halfEven";
8
+ type UseGroupingType = "min2" | "auto" | "always" | boolean;
9
+ interface NumberFormatDigitOptions {
10
+ minimumIntegerDigits?: number;
11
+ minimumSignificantDigits?: number;
12
+ maximumSignificantDigits?: number;
13
+ minimumFractionDigits?: number;
14
+ maximumFractionDigits?: number;
15
+ roundingPriority?: RoundingPriorityType;
16
+ roundingIncrement?: number;
17
+ roundingMode?: RoundingModeType;
18
+ trailingZeroDisplay?: TrailingZeroDisplay;
19
+ }
20
+ type NumberFormatOptionsLocaleMatcher = "lookup" | "best fit";
21
+ type NumberFormatOptionsStyle = "decimal" | "percent" | "currency" | "unit";
22
+ type NumberFormatOptionsCompactDisplay = "short" | "long";
23
+ type NumberFormatOptionsCurrencyDisplay = "symbol" | "code" | "name" | "narrowSymbol";
24
+ type NumberFormatOptionsCurrencySign = "standard" | "accounting";
25
+ type NumberFormatOptionsNotation = NumberFormatNotation;
26
+ type NumberFormatOptionsSignDisplay = "auto" | "always" | "never" | "exceptZero" | "negative";
27
+ type NumberFormatOptionsUnitDisplay = "long" | "short" | "narrow";
28
+ type TrailingZeroDisplay = "auto" | "stripIfInteger";
29
+ type NumberFormatOptions = Omit<Intl.NumberFormatOptions, "signDisplay" | "useGrouping"> & NumberFormatDigitOptions & {
30
+ localeMatcher?: NumberFormatOptionsLocaleMatcher;
31
+ style?: NumberFormatOptionsStyle;
32
+ compactDisplay?: NumberFormatOptionsCompactDisplay;
33
+ currencyDisplay?: NumberFormatOptionsCurrencyDisplay;
34
+ currencySign?: NumberFormatOptionsCurrencySign;
35
+ notation?: NumberFormatOptionsNotation;
36
+ signDisplay?: NumberFormatOptionsSignDisplay;
37
+ unit?: string;
38
+ unitDisplay?: NumberFormatOptionsUnitDisplay;
39
+ numberingSystem?: string;
40
+ trailingZeroDisplay?: TrailingZeroDisplay;
41
+ roundingPriority?: RoundingPriorityType;
42
+ roundingIncrement?: number;
43
+ roundingMode?: RoundingModeType;
44
+ useGrouping?: UseGroupingType;
45
+ };
46
+ //#endregion
47
+ //#region packages/intl/error.d.ts
48
+ declare enum IntlErrorCode {
49
+ FORMAT_ERROR = "FORMAT_ERROR",
50
+ UNSUPPORTED_FORMATTER = "UNSUPPORTED_FORMATTER",
51
+ INVALID_CONFIG = "INVALID_CONFIG",
52
+ MISSING_DATA = "MISSING_DATA",
53
+ MISSING_TRANSLATION = "MISSING_TRANSLATION"
54
+ }
55
+ declare class IntlError<T extends IntlErrorCode = IntlErrorCode.FORMAT_ERROR> extends Error {
56
+ readonly code: T;
57
+ constructor(code: T, message: string, exception?: Error | unknown);
58
+ }
59
+ declare class UnsupportedFormatterError extends IntlError<IntlErrorCode.UNSUPPORTED_FORMATTER> {
60
+ constructor(message: string, exception?: Error | unknown);
61
+ }
62
+ declare class InvalidConfigError extends IntlError<IntlErrorCode.INVALID_CONFIG> {
63
+ constructor(message: string, exception?: Error | unknown);
64
+ }
65
+ declare class MissingDataError extends IntlError<IntlErrorCode.MISSING_DATA> {
66
+ constructor(message: string, exception?: Error | unknown);
67
+ }
68
+ declare class IntlFormatError extends IntlError<IntlErrorCode.FORMAT_ERROR> {
69
+ readonly descriptor?: MessageDescriptor;
70
+ readonly locale: string;
71
+ constructor(message: string, locale: string, exception?: Error | unknown);
72
+ }
73
+ declare class MessageFormatError extends IntlFormatError {
74
+ readonly descriptor?: MessageDescriptor;
75
+ readonly locale: string;
76
+ constructor(message: string, locale: string, descriptor?: MessageDescriptor, exception?: Error | unknown);
77
+ }
78
+ declare class MissingTranslationError extends IntlError<IntlErrorCode.MISSING_TRANSLATION> {
79
+ readonly descriptor?: MessageDescriptor;
80
+ constructor(descriptor: MessageDescriptor, locale: string);
81
+ }
82
+ //#endregion
83
+ //#region packages/intl/utils.d.ts
84
+ declare function filterProps<T extends Record<string, any>, K extends string>(props: T, allowlist: Array<K>, defaults?: Partial<T>): Pick<T, K>;
85
+ declare const DEFAULT_INTL_CONFIG: Pick<ResolvedIntlConfig<any>, "fallbackOnEmptyString" | "formats" | "messages" | "timeZone" | "defaultLocale" | "defaultFormats" | "onError" | "onWarn">;
86
+ declare function createIntlCache(): IntlCache;
87
+ /**
88
+ * Create intl formatters and populate cache
89
+ * @param cache explicit cache to prevent leaking memory
90
+ */
91
+ declare function createFormatters(cache?: IntlCache): Formatters;
92
+ declare function getNamedFormat<T extends keyof CustomFormats>(formats: CustomFormats, type: T, name: string, onError: OnErrorFn): NumberFormatOptions | Intl.DateTimeFormatOptions | Intl.RelativeTimeFormatOptions | undefined;
93
+ //#endregion
94
+ //#region packages/intl/types.d.ts
95
+ interface Part<T = string> {
96
+ type: "element" | "literal";
97
+ value: T;
98
+ }
99
+ declare global {
100
+ namespace FormatjsIntl {
101
+ interface Message {}
102
+ interface IntlConfig {}
103
+ interface Formats {}
104
+ }
105
+ }
106
+ type MessageIds = FormatjsIntl.Message extends {
107
+ ids: infer T;
108
+ } ? T extends string ? T : string : string;
109
+ type Locale = FormatjsIntl.IntlConfig extends {
110
+ locale: infer T;
111
+ } ? T extends string ? T : string : string;
112
+ type OnErrorFn = (err: MissingTranslationError | MessageFormatError | MissingDataError | InvalidConfigError | UnsupportedFormatterError | FormatError) => void;
113
+ type OnWarnFn = (warning: string) => void;
114
+ /**
115
+ * Config for intl object.
116
+ * Generic type T is the type of potential rich text element. For example:
117
+ * With React, T would be React.ReactNode
118
+ */
119
+ interface ResolvedIntlConfig<T = string> {
120
+ locale: Locale;
121
+ timeZone?: string;
122
+ fallbackOnEmptyString?: boolean;
123
+ formats: CustomFormats;
124
+ messages: Record<MessageIds, string> | Record<MessageIds, MessageFormatElement[]>;
125
+ defaultLocale: string;
126
+ defaultFormats: CustomFormats;
127
+ defaultRichTextElements?: Record<string, FormatXMLElementFn<T>>;
128
+ onError: OnErrorFn;
129
+ onWarn?: OnWarnFn;
130
+ }
131
+ interface CustomFormats extends Partial<Formats> {
132
+ relative?: Record<string, Intl.RelativeTimeFormatOptions>;
133
+ dateTimeRange?: Record<string, Intl.DateTimeFormatOptions>;
134
+ }
135
+ interface CustomFormatConfig<Source = string> {
136
+ format?: Source extends keyof FormatjsIntl.Formats ? FormatjsIntl.Formats[Source] : string;
137
+ }
138
+ type FormatDateTimeRangeOptions = Omit<Intl.DateTimeFormatOptions, "localeMatcher"> & CustomFormatConfig<"dateTimeRange">;
139
+ type FormatDateOptions = Omit<Intl.DateTimeFormatOptions, "localeMatcher"> & CustomFormatConfig<"date">;
140
+ type FormatTimeOptions = Omit<Intl.DateTimeFormatOptions, "localeMatcher"> & CustomFormatConfig<"time">;
141
+ type FormatNumberOptions = Omit<NumberFormatOptions, "localeMatcher"> & CustomFormatConfig<"number">;
142
+ type FormatRelativeTimeOptions = Omit<Intl.RelativeTimeFormatOptions, "localeMatcher"> & CustomFormatConfig<"time">;
143
+ type FormatPluralOptions = Omit<Intl.PluralRulesOptions, "localeMatcher"> & CustomFormatConfig;
144
+ type FormatListOptions = Omit<Intl.ListFormatOptions, "localeMatcher">;
145
+ type FormatDisplayNameOptions = Omit<Intl.DisplayNamesOptions, "localeMatcher">;
146
+ /**
147
+ * `TBase` is the type constraints of the rich text element in the formatted output.
148
+ * For example, with React, `TBase` should be `React.ReactNode`.
149
+ */
150
+ interface IntlFormatters<TBase = unknown> {
151
+ formatDateTimeRange(this: void, from: Parameters<Intl.DateTimeFormat["formatRange"]>[0] | string, to: Parameters<Intl.DateTimeFormat["formatRange"]>[1] | string, opts?: FormatDateTimeRangeOptions): string;
152
+ formatDate(this: void, value: Parameters<Intl.DateTimeFormat["format"]>[0] | string, opts?: FormatDateOptions): string;
153
+ formatTime(this: void, value: Parameters<Intl.DateTimeFormat["format"]>[0] | string, opts?: FormatTimeOptions): string;
154
+ formatDateToParts(this: void, value: Parameters<Intl.DateTimeFormat["format"]>[0] | string, opts?: FormatDateOptions): Intl.DateTimeFormatPart[];
155
+ formatTimeToParts(this: void, value: Parameters<Intl.DateTimeFormat["format"]>[0] | string, opts?: FormatDateOptions): Intl.DateTimeFormatPart[];
156
+ formatRelativeTime(this: void, value: Parameters<Intl.RelativeTimeFormat["format"]>[0], unit?: Parameters<Intl.RelativeTimeFormat["format"]>[1], opts?: FormatRelativeTimeOptions): string;
157
+ formatNumber(this: void, value: Parameters<Intl.NumberFormat["format"]>[0], opts?: FormatNumberOptions): string;
158
+ formatNumberToParts(this: void, value: Parameters<Intl.NumberFormat["format"]>[0], opts?: FormatNumberOptions): Intl.NumberFormatPart[];
159
+ formatPlural(this: void, value: Parameters<Intl.PluralRules["select"]>[0], opts?: FormatPluralOptions): ReturnType<Intl.PluralRules["select"]>;
160
+ formatMessage(this: void, descriptor: MessageDescriptor, values?: Record<string, PrimitiveType | FormatXMLElementFn<string, string>>, opts?: Options): string;
161
+ formatMessage<T extends TBase, TValue extends T | FormatXMLElementFn<T>>(this: void, descriptor: MessageDescriptor, values?: Record<string, PrimitiveType | TValue>, opts?: Options): string | T | Array<string | T>;
162
+ $t(this: void, descriptor: MessageDescriptor, values?: Record<string, PrimitiveType | FormatXMLElementFn<string, string>>, opts?: Options): string;
163
+ $t<T extends TBase>(this: void, descriptor: MessageDescriptor, values?: Record<string, PrimitiveType | T | FormatXMLElementFn<T>>, opts?: Options): string | T | (T | string)[];
164
+ formatList(this: void, values: Iterable<string>, opts?: FormatListOptions): string;
165
+ formatList<T extends TBase>(this: void, values: Iterable<string | T>, opts?: FormatListOptions): T | string | (string | T)[];
166
+ formatListToParts<T extends TBase>(this: void, values: Iterable<string | T>, opts?: FormatListOptions): Part[];
167
+ formatDisplayName(this: void, value: Parameters<Intl.DisplayNames["of"]>[0], opts: FormatDisplayNameOptions): string | undefined;
168
+ }
169
+ interface Formatters {
170
+ getDateTimeFormat(this: void, ...args: ConstructorParameters<typeof Intl.DateTimeFormat>): Intl.DateTimeFormat;
171
+ getNumberFormat(this: void, locales?: string | string[], opts?: NumberFormatOptions): Intl.NumberFormat;
172
+ getMessageFormat(this: void, ...args: ConstructorParameters<typeof IntlMessageFormat>): IntlMessageFormat;
173
+ getRelativeTimeFormat(this: void, ...args: ConstructorParameters<typeof Intl.RelativeTimeFormat>): Intl.RelativeTimeFormat;
174
+ getPluralRules(this: void, ...args: ConstructorParameters<typeof Intl.PluralRules>): Intl.PluralRules;
175
+ getListFormat(this: void, ...args: ConstructorParameters<typeof Intl.ListFormat>): Intl.ListFormat;
176
+ getDisplayNames(this: void, ...args: ConstructorParameters<typeof Intl.DisplayNames>): Intl.DisplayNames;
177
+ }
178
+ interface IntlShape<T = string> extends ResolvedIntlConfig<T>, IntlFormatters<T> {
179
+ formatters: Formatters;
180
+ }
181
+ interface IntlCache {
182
+ dateTime: Record<string, Intl.DateTimeFormat>;
183
+ number: Record<string, Intl.NumberFormat>;
184
+ message: Record<string, IntlMessageFormat>;
185
+ relativeTime: Record<string, Intl.RelativeTimeFormat>;
186
+ pluralRules: Record<string, Intl.PluralRules>;
187
+ list: Record<string, Intl.ListFormat>;
188
+ displayNames: Record<string, Intl.DisplayNames>;
189
+ }
190
+ interface MessageDescriptor {
191
+ id?: MessageIds;
192
+ description?: string | object;
193
+ defaultMessage?: string | MessageFormatElement[];
194
+ }
195
+ type IntlConfig<T = string> = Omit<ResolvedIntlConfig<T>, keyof typeof DEFAULT_INTL_CONFIG> & Partial<typeof DEFAULT_INTL_CONFIG>;
196
+ //#endregion
197
+ //#region packages/intl/message.d.ts
198
+ type FormatMessageFn<T> = ({
199
+ locale,
200
+ formats,
201
+ messages,
202
+ defaultLocale,
203
+ defaultFormats,
204
+ fallbackOnEmptyString,
205
+ onError,
206
+ timeZone,
207
+ defaultRichTextElements
208
+ }: {
209
+ locale: string;
210
+ timeZone?: string;
211
+ formats: CustomFormats;
212
+ messages: Record<string, string> | Record<string, MessageFormatElement[]>;
213
+ defaultLocale: string;
214
+ defaultFormats: CustomFormats;
215
+ defaultRichTextElements?: Record<string, FormatXMLElementFn<T>>;
216
+ fallbackOnEmptyString?: boolean;
217
+ onError: OnErrorFn;
218
+ }, state: Formatters$1 & Pick<Formatters, "getMessageFormat">, messageDescriptor: MessageDescriptor, values?: Record<string, PrimitiveType | T | FormatXMLElementFn<T>>, opts?: Options) => T extends string ? string : Array<T | string> | string | T;
219
+ declare const formatMessage: FormatMessageFn<any>;
220
+ //#endregion
221
+ //#region packages/intl/dateTime.d.ts
222
+ declare function formatDate(config: {
223
+ locale: string;
224
+ timeZone?: string;
225
+ formats: CustomFormats;
226
+ onError: OnErrorFn;
227
+ }, getDateTimeFormat: Formatters["getDateTimeFormat"], value: Parameters<IntlFormatters["formatDate"]>[0], options?: Parameters<IntlFormatters["formatDate"]>[1]): string;
228
+ declare function formatTime(config: {
229
+ locale: string;
230
+ timeZone?: string;
231
+ formats: CustomFormats;
232
+ onError: OnErrorFn;
233
+ }, getDateTimeFormat: Formatters["getDateTimeFormat"], value: Parameters<IntlFormatters["formatTime"]>[0], options?: Parameters<IntlFormatters["formatTime"]>[1]): string;
234
+ declare function formatDateToParts(config: {
235
+ locale: string;
236
+ timeZone?: string;
237
+ formats: CustomFormats;
238
+ onError: OnErrorFn;
239
+ }, getDateTimeFormat: Formatters["getDateTimeFormat"], value: Parameters<IntlFormatters["formatDate"]>[0], options?: Parameters<IntlFormatters["formatDate"]>[1]): Intl.DateTimeFormatPart[];
240
+ declare function formatTimeToParts(config: {
241
+ locale: string;
242
+ timeZone?: string;
243
+ formats: CustomFormats;
244
+ onError: OnErrorFn;
245
+ }, getDateTimeFormat: Formatters["getDateTimeFormat"], value: Parameters<IntlFormatters["formatTimeToParts"]>[0], options?: Parameters<IntlFormatters["formatTimeToParts"]>[1]): Intl.DateTimeFormatPart[];
246
+ //#endregion
247
+ //#region packages/intl/displayName.d.ts
248
+ declare function formatDisplayName({
249
+ locale,
250
+ onError
251
+ }: {
252
+ locale: string;
253
+ onError: OnErrorFn;
254
+ }, getDisplayNames: Formatters["getDisplayNames"], value: Parameters<IntlFormatters["formatDisplayName"]>[0], options: Parameters<IntlFormatters["formatDisplayName"]>[1]): string | undefined;
255
+ //#endregion
256
+ //#region packages/intl/list.d.ts
257
+ declare function formatList(opts: {
258
+ locale: string;
259
+ onError: OnErrorFn;
260
+ }, getListFormat: Formatters["getListFormat"], values: Iterable<string>, options: Parameters<IntlFormatters["formatList"]>[1]): string;
261
+ //#endregion
262
+ //#region packages/intl/plural.d.ts
263
+ declare function formatPlural({
264
+ locale,
265
+ onError
266
+ }: {
267
+ locale: string;
268
+ onError: OnErrorFn;
269
+ }, getPluralRules: Formatters["getPluralRules"], value: Parameters<IntlFormatters["formatPlural"]>[0], options?: Parameters<IntlFormatters["formatPlural"]>[1]): Intl.LDMLPluralRule;
270
+ //#endregion
271
+ //#region packages/intl/relativeTime.d.ts
272
+ declare function formatRelativeTime(config: {
273
+ locale: string;
274
+ formats: CustomFormats;
275
+ onError: OnErrorFn;
276
+ }, getRelativeTimeFormat: Formatters["getRelativeTimeFormat"], value: Parameters<IntlFormatters["formatRelativeTime"]>[0], unit?: Parameters<IntlFormatters["formatRelativeTime"]>[1], options?: Parameters<IntlFormatters["formatRelativeTime"]>[2]): string;
277
+ //#endregion
278
+ //#region packages/intl/number.d.ts
279
+ declare function formatNumber(config: {
280
+ locale: string;
281
+ formats: CustomFormats;
282
+ onError: OnErrorFn;
283
+ }, getNumberFormat: Formatters["getNumberFormat"], value: Parameters<IntlFormatters["formatNumber"]>[0], options?: Parameters<IntlFormatters["formatNumber"]>[1]): string;
284
+ declare function formatNumberToParts(config: {
285
+ locale: string;
286
+ formats: CustomFormats;
287
+ onError: OnErrorFn;
288
+ }, getNumberFormat: Formatters["getNumberFormat"], value: Parameters<IntlFormatters["formatNumber"]>[0], options?: Parameters<IntlFormatters["formatNumber"]>[1]): Intl.NumberFormatPart[];
289
+ //#endregion
290
+ //#region packages/intl/create-intl.d.ts
291
+ interface CreateIntlFn<T = string, C extends IntlConfig<T> = IntlConfig<T>, S extends IntlShape<T> = IntlShape<T>> {
292
+ (config: C, cache?: IntlCache): S;
293
+ }
294
+ /**
295
+ * Create intl object
296
+ * @param config intl config
297
+ * @param cache cache for formatter instances to prevent memory leak
298
+ */
299
+ declare function createIntl<T = string>(config: IntlConfig<T>, cache?: IntlCache): IntlShape<T>;
300
+ //#endregion
301
+ //#region packages/intl/index.d.ts
302
+ declare function defineMessages<K extends keyof any, T = MessageDescriptor, U extends Record<K, T> = Record<K, T>>(msgs: U): U;
303
+ declare function defineMessage<T>(msg: T): T;
304
+ //#endregion
305
+ export { type CreateIntlFn, CustomFormatConfig, CustomFormats, DEFAULT_INTL_CONFIG, FormatDateOptions, FormatDateTimeRangeOptions, FormatDisplayNameOptions, FormatListOptions, type FormatMessageFn, FormatNumberOptions, FormatPluralOptions, FormatRelativeTimeOptions, FormatTimeOptions, Formatters, IntlCache, IntlConfig, IntlError, IntlErrorCode, IntlFormatError, IntlFormatters, IntlShape, InvalidConfigError, MessageDescriptor, MessageFormatError, MissingDataError, MissingTranslationError, OnErrorFn, OnWarnFn, Part, ResolvedIntlConfig, UnsupportedFormatterError, createFormatters, createIntl, createIntlCache, defineMessage, defineMessages, filterProps, formatDate, formatDateToParts, formatDisplayName, formatList, formatMessage, formatNumber, formatNumberToParts, formatPlural, formatRelativeTime, formatTime, formatTimeToParts, getNamedFormat };
306
+ //# sourceMappingURL=index.d.ts.map