UNPKG

3.02 kBTypeScriptView Raw
1import { ReactElement } from 'react';
2import { Trans } from './index.js';
3import type { Namespace, TypeOptions, i18n, ParseKeys } from 'i18next';
4
5export { Trans };
6
7type _DefaultNamespace = TypeOptions['defaultNS'];
8declare module 'react-i18next/icu.macro' {
9 export interface PluralSubProps<
10 Key extends ParseKeys<Ns, {}, ''>,
11 Ns extends Namespace = _DefaultNamespace,
12 > {
13 children?: never;
14 i18nKey?: Key;
15 i18n?: i18n;
16 ns?: Ns;
17 count: number;
18 values?: {};
19 zero?: string | ReactElement;
20 one?: string | ReactElement;
21 two?: string | ReactElement;
22 few?: string | ReactElement;
23 many?: string | ReactElement;
24 other: string | ReactElement;
25 }
26
27 type PluralProps<
28 T,
29 Key extends ParseKeys<Ns, {}, ''>,
30 Ns extends Namespace = _DefaultNamespace,
31 > = {
32 [P in keyof T]: P extends keyof PluralSubProps<Key, Ns>
33 ? // support the standard properties of Plural
34 PluralSubProps<Key, Ns>[P]
35 : // this supports infinite $0={..} or $123={..}
36 // technically it also supports $-1={..} and $2.3={..} but we don't need to
37 // worry since that's invalid syntax.
38 P extends `$${number}`
39 ? string | ReactElement
40 : never;
41 };
42
43 interface SelectSubProps {
44 [key: string]: string | ReactElement;
45 }
46
47 interface NoChildren {
48 children?: never;
49 }
50
51 interface SelectRequiredProps<
52 Key extends ParseKeys<Ns, {}, ''>,
53 Ns extends Namespace = _DefaultNamespace,
54 > extends NoChildren {
55 i18nKey?: Key;
56 i18n?: i18n;
57 ns?: Ns;
58 other: string | ReactElement;
59 }
60
61 // defining it this way ensures that `other` is always defined, but allows
62 // unlimited other select types.
63 type SelectProps<
64 Key extends ParseKeys<Ns, {}, ''>,
65 Ns extends Namespace = _DefaultNamespace,
66 > = SelectSubProps & SelectRequiredProps<Key, Ns>;
67
68 function Plural<T, Key extends ParseKeys<Ns, {}, ''>, Ns extends Namespace = _DefaultNamespace>(
69 props: PluralProps<T, Key, Ns> & NoChildren,
70 ): ReactElement;
71
72 function SelectOrdinal<
73 T,
74 Key extends ParseKeys<Ns, {}, ''>,
75 Ns extends Namespace = _DefaultNamespace,
76 >(props: PluralProps<T, Key, Ns> & NoChildren): ReactElement;
77
78 function Select<Key extends ParseKeys<Ns, {}, ''>, Ns extends Namespace = _DefaultNamespace>(
79 props: SelectProps<Key, Ns>,
80 ): ReactElement;
81
82 function date(strings: TemplateStringsArray, variable: Date): string;
83 function time(strings: TemplateStringsArray, variable: Date): string;
84 function number(strings: TemplateStringsArray, variable: number): string;
85
86 type ValidInterpolations = ReactElement | string;
87
88 function plural(
89 strings: TemplateStringsArray,
90 variable: number,
91 ...args: ValidInterpolations[]
92 ): string;
93 function selectOrdinal(
94 strings: TemplateStringsArray,
95 variable: number,
96 ...args: ValidInterpolations[]
97 ): string;
98 function select(
99 strings: TemplateStringsArray,
100 variable: string,
101 ...args: ValidInterpolations[]
102 ): string;
103}