import { MessageFormatElement } from '@formatjs/icu-messageformat-parser'; import { NumberFormatOptions } from '@formatjs/ecma402-abstract'; import { FormatError, Formats, FormatXMLElementFn, IntlMessageFormat, Options as IntlMessageFormatOptions, PrimitiveType } from 'intl-messageformat'; import { InvalidConfigError, MessageFormatError, MissingDataError, MissingTranslationError, UnsupportedFormatterError } from './error.js'; import { DEFAULT_INTL_CONFIG } from './utils.js'; export interface Part { type: 'element' | 'literal'; value: T; } declare global { namespace FormatjsIntl { interface Message { } interface IntlConfig { } interface Formats { } } } type MessageIds = FormatjsIntl.Message extends { ids: infer T; } ? T extends string ? T : string : string; type Locale = FormatjsIntl.IntlConfig extends { locale: infer T; } ? T extends string ? T : string : string; export type OnErrorFn = (err: MissingTranslationError | MessageFormatError | MissingDataError | InvalidConfigError | UnsupportedFormatterError | FormatError) => void; export type OnWarnFn = (warning: string) => void; /** * Config for intl object. * Generic type T is the type of potential rich text element. For example: * With React, T would be React.ReactNode */ export interface ResolvedIntlConfig { locale: Locale; timeZone?: string; fallbackOnEmptyString?: boolean; formats: CustomFormats; messages: Record | Record; defaultLocale: string; defaultFormats: CustomFormats; defaultRichTextElements?: Record>; onError: OnErrorFn; onWarn?: OnWarnFn; } export interface CustomFormats extends Partial { relative?: Record; dateTimeRange?: Record; } export interface CustomFormatConfig { format?: Source extends keyof FormatjsIntl.Formats ? FormatjsIntl.Formats[Source] : string; } export type FormatDateTimeRangeOptions = Omit & CustomFormatConfig<'dateTimeRange'>; export type FormatDateOptions = Omit & CustomFormatConfig<'date'>; export type FormatTimeOptions = Omit & CustomFormatConfig<'time'>; export type FormatNumberOptions = Omit & CustomFormatConfig<'number'>; export type FormatRelativeTimeOptions = Omit & CustomFormatConfig<'time'>; export type FormatPluralOptions = Omit & CustomFormatConfig; export type FormatListOptions = Omit; export type FormatDisplayNameOptions = Omit; /** * `TBase` is the type constraints of the rich text element in the formatted output. * For example, with React, `TBase` should be `React.ReactNode`. */ export interface IntlFormatters { formatDateTimeRange(this: void, from: Parameters[0] | string, to: Parameters[1] | string, opts?: FormatDateTimeRangeOptions): string; formatDate(this: void, value: Parameters[0] | string, opts?: FormatDateOptions): string; formatTime(this: void, value: Parameters[0] | string, opts?: FormatTimeOptions): string; formatDateToParts(this: void, value: Parameters[0] | string, opts?: FormatDateOptions): Intl.DateTimeFormatPart[]; formatTimeToParts(this: void, value: Parameters[0] | string, opts?: FormatDateOptions): Intl.DateTimeFormatPart[]; formatRelativeTime(this: void, value: Parameters[0], unit?: Parameters[1], opts?: FormatRelativeTimeOptions): string; formatNumber(this: void, value: Parameters[0], opts?: FormatNumberOptions): string; formatNumberToParts(this: void, value: Parameters[0], opts?: FormatNumberOptions): Intl.NumberFormatPart[]; formatPlural(this: void, value: Parameters[0], opts?: FormatPluralOptions): ReturnType; formatMessage(this: void, descriptor: MessageDescriptor, values?: Record>, opts?: IntlMessageFormatOptions): string; formatMessage>(this: void, descriptor: MessageDescriptor, values?: Record, opts?: IntlMessageFormatOptions): string | T | Array; $t(this: void, descriptor: MessageDescriptor, values?: Record>, opts?: IntlMessageFormatOptions): string; $t(this: void, descriptor: MessageDescriptor, values?: Record>, opts?: IntlMessageFormatOptions): string | T | (T | string)[]; formatList(this: void, values: Iterable, opts?: FormatListOptions): string; formatList(this: void, values: Iterable, opts?: FormatListOptions): T | string | (string | T)[]; formatListToParts(this: void, values: Iterable, opts?: FormatListOptions): Part[]; formatDisplayName(this: void, value: Parameters[0], opts: FormatDisplayNameOptions): string | undefined; } export interface Formatters { getDateTimeFormat(this: void, ...args: ConstructorParameters): Intl.DateTimeFormat; getNumberFormat(this: void, locales?: string | string[], opts?: NumberFormatOptions): Intl.NumberFormat; getMessageFormat(this: void, ...args: ConstructorParameters): IntlMessageFormat; getRelativeTimeFormat(this: void, ...args: ConstructorParameters): Intl.RelativeTimeFormat; getPluralRules(this: void, ...args: ConstructorParameters): Intl.PluralRules; getListFormat(this: void, ...args: ConstructorParameters): Intl.ListFormat; getDisplayNames(this: void, ...args: ConstructorParameters): Intl.DisplayNames; } export interface IntlShape extends ResolvedIntlConfig, IntlFormatters { formatters: Formatters; } export interface IntlCache { dateTime: Record; number: Record; message: Record; relativeTime: Record; pluralRules: Record; list: Record; displayNames: Record; } export interface MessageDescriptor { id?: MessageIds; description?: string | object; defaultMessage?: string | MessageFormatElement[]; } export type IntlConfig = Omit, keyof typeof DEFAULT_INTL_CONFIG> & Partial; export {};