UNPKG

2.19 kBTypeScriptView Raw
1import type { i18n, ParseKeys, Namespace, TypeOptions, TOptions, TFunction } from 'i18next';
2import * as React from 'react';
3
4type _DefaultNamespace = TypeOptions['defaultNS'];
5
6type TransChild = React.ReactNode | Record<string, unknown>;
7export type TransProps<
8 Key extends ParseKeys<Ns, TOpt, KPrefix>,
9 Ns extends Namespace = _DefaultNamespace,
10 KPrefix = undefined,
11 TContext extends string | undefined = undefined,
12 TOpt extends TOptions & { context?: TContext } = { context: TContext },
13 E = React.HTMLProps<HTMLDivElement>,
14> = E & {
15 children?: TransChild | readonly TransChild[];
16 components?: readonly React.ReactElement[] | { readonly [tagName: string]: React.ReactElement };
17 count?: number;
18 context?: TContext;
19 defaults?: string;
20 i18n?: i18n;
21 i18nKey?: Key | Key[];
22 ns?: Ns;
23 parent?: string | React.ComponentType<any> | null; // used in React.createElement if not null
24 tOptions?: TOpt;
25 values?: {};
26 shouldUnescape?: boolean;
27 t?: TFunction<Ns, KPrefix>;
28};
29
30export function Trans<
31 Key extends ParseKeys<Ns, TOpt, KPrefix>,
32 Ns extends Namespace = _DefaultNamespace,
33 KPrefix = undefined,
34 TContext extends string | undefined = undefined,
35 TOpt extends TOptions & { context?: TContext } = { context: TContext },
36 E = React.HTMLProps<HTMLDivElement>,
37>(props: TransProps<Key, Ns, KPrefix, TContext, TOpt, E>): React.ReactElement;
38
39export type ErrorCode =
40 | 'NO_I18NEXT_INSTANCE'
41 | 'NO_LANGUAGES'
42 | 'DEPRECATED_OPTION'
43 | 'TRANS_NULL_VALUE'
44 | 'TRANS_INVALID_OBJ'
45 | 'TRANS_INVALID_VAR'
46 | 'TRANS_INVALID_COMPONENTS';
47
48export type ErrorMeta = {
49 code: ErrorCode;
50 i18nKey?: string;
51 [x: string]: any;
52};
53
54/**
55 * Use to type the logger arguments
56 * @example
57 * ```
58 * import type { ErrorArgs } from 'react-i18next';
59 *
60 * const logger = {
61 * // ....
62 * warn: function (...args: ErrorArgs) {
63 * if (args[1]?.code === 'TRANS_INVALID_OBJ') {
64 * const [msg, { i18nKey, ...rest }] = args;
65 * return log(i18nKey, msg, rest);
66 * }
67 * log(...args);
68 * }
69 * }
70 * i18n.use(logger).use(i18nReactPlugin).init({...});
71 * ```
72 */
73export type ErrorArgs = readonly [string, ErrorMeta | undefined, ...any[]];