-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
types.ts
189 lines (176 loc) · 5.4 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import {
IntlMessageFormat,
Formats,
PrimitiveType,
FormatXMLElementFn,
FormatError,
} from 'intl-messageformat';
import IntlRelativeTimeFormat from '@formatjs/intl-relativetimeformat';
import {MessageFormatElement} from 'intl-messageformat-parser';
import IntlListFormat, {IntlListFormatOptions} from '@formatjs/intl-listformat';
import {DisplayNames, DisplayNamesOptions} from '@formatjs/intl-displaynames';
import {
MissingTranslationError,
MessageFormatError,
MissingDataError,
InvalidConfigError,
UnsupportedFormatterError,
} from './error';
import {DEFAULT_INTL_CONFIG} from './utils';
import {
IntlRelativeTimeFormatOptions,
DateTimeFormatOptions,
NumberFormatOptions,
} from '@formatjs/ecma402-abstract';
export type OnErrorFn = (
err:
| MissingTranslationError
| MessageFormatError
| MissingDataError
| InvalidConfigError
| UnsupportedFormatterError
| FormatError
) => 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 IntlConfig<T = string> {
locale: string;
timeZone?: string;
formats: CustomFormats;
messages: Record<string, string> | Record<string, MessageFormatElement[]>;
defaultLocale: string;
defaultFormats: CustomFormats;
defaultRichTextElements?: Record<string, FormatXMLElementFn<T>>;
onError: OnErrorFn;
}
export interface CustomFormats extends Partial<Formats> {
relative?: Record<string, IntlRelativeTimeFormatOptions>;
}
export interface CustomFormatConfig {
format?: string;
}
export type FormatDateOptions = Exclude<
DateTimeFormatOptions,
'localeMatcher'
> &
CustomFormatConfig;
export type FormatNumberOptions = Exclude<
NumberFormatOptions,
'localeMatcher'
> &
CustomFormatConfig;
export type FormatRelativeTimeOptions = Exclude<
IntlRelativeTimeFormatOptions,
'localeMatcher'
> &
CustomFormatConfig;
export type FormatPluralOptions = Exclude<
Intl.PluralRulesOptions,
'localeMatcher'
> &
CustomFormatConfig;
export type FormatListOptions = Exclude<IntlListFormatOptions, 'localeMatcher'>;
export type FormatDisplayNameOptions = Exclude<
DisplayNamesOptions,
'localeMatcher'
>;
export interface IntlFormatters<T = any, R = T> {
formatDate(
value: Parameters<Intl.DateTimeFormat['format']>[0] | string,
opts?: FormatDateOptions
): string;
formatTime(
value: Parameters<Intl.DateTimeFormat['format']>[0] | string,
opts?: FormatDateOptions
): string;
formatDateToParts(
value: Parameters<Intl.DateTimeFormat['format']>[0] | string,
opts?: FormatDateOptions
): Intl.DateTimeFormatPart[];
formatTimeToParts(
value: Parameters<Intl.DateTimeFormat['format']>[0] | string,
opts?: FormatDateOptions
): Intl.DateTimeFormatPart[];
formatRelativeTime(
value: Parameters<IntlRelativeTimeFormat['format']>[0],
unit?: Parameters<IntlRelativeTimeFormat['format']>[1],
opts?: FormatRelativeTimeOptions
): string;
formatNumber(
value: Parameters<Intl.NumberFormat['format']>[0],
opts?: FormatNumberOptions
): string;
formatNumberToParts(
value: Parameters<Intl.NumberFormat['format']>[0],
opts?: FormatNumberOptions
): Intl.NumberFormatPart[];
formatPlural(
value: Parameters<Intl.PluralRules['select']>[0],
opts?: FormatPluralOptions
): ReturnType<Intl.PluralRules['select']>;
formatMessage(
descriptor: MessageDescriptor,
values?: Record<string, PrimitiveType | FormatXMLElementFn<string, string>>
): string;
formatMessage(
descriptor: MessageDescriptor,
values?: Record<string, PrimitiveType | T | FormatXMLElementFn<T, R>>
): R;
formatList(values: Array<string>, opts?: FormatListOptions): string;
formatList(
values: Array<string | T>,
opts?: FormatListOptions
): T | string | Array<string | T>;
formatDisplayName(
value: Parameters<DisplayNames['of']>[0],
opts: FormatDisplayNameOptions
): string | undefined;
}
export interface Formatters {
getDateTimeFormat(
...args: ConstructorParameters<typeof Intl.DateTimeFormat>
): Intl.DateTimeFormat;
getNumberFormat(
...args: ConstructorParameters<typeof Intl.NumberFormat>
): Intl.NumberFormat;
getMessageFormat(
...args: ConstructorParameters<typeof IntlMessageFormat>
): IntlMessageFormat;
getRelativeTimeFormat(
...args: ConstructorParameters<typeof IntlRelativeTimeFormat>
): IntlRelativeTimeFormat;
getPluralRules(
...args: ConstructorParameters<typeof Intl.PluralRules>
): Intl.PluralRules;
getListFormat(
...args: ConstructorParameters<typeof IntlListFormat>
): IntlListFormat;
getDisplayNames(
...args: ConstructorParameters<typeof DisplayNames>
): DisplayNames;
}
export interface IntlShape<T> extends IntlConfig<T>, IntlFormatters {
formatters: Formatters;
}
export interface IntlCache {
dateTime: Record<string, Intl.DateTimeFormat>;
number: Record<string, Intl.NumberFormat>;
message: Record<string, IntlMessageFormat>;
relativeTime: Record<string, IntlRelativeTimeFormat>;
pluralRules: Record<string, Intl.PluralRules>;
list: Record<string, IntlListFormat>;
displayNames: Record<string, DisplayNames>;
}
export interface MessageDescriptor {
id?: string | number;
description?: string | object;
defaultMessage?: string | MessageFormatElement[];
}
export type OptionalIntlConfig<T = string> = Omit<
IntlConfig<T>,
keyof typeof DEFAULT_INTL_CONFIG
> &
Partial<typeof DEFAULT_INTL_CONFIG>;