TransWithoutContext.d.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import type {
  2. i18n,
  3. ApplyTarget,
  4. ConstrainTarget,
  5. GetSource,
  6. ParseKeys,
  7. Namespace,
  8. SelectorFn,
  9. TypeOptions,
  10. TOptions,
  11. TFunction,
  12. } from 'i18next';
  13. import * as React from 'react';
  14. type _DefaultNamespace = TypeOptions['defaultNS'];
  15. type _EnableSelector = TypeOptions['enableSelector'];
  16. type TransChild = React.ReactNode | Record<string, unknown>;
  17. type $NoInfer<T> = [T][T extends T ? 0 : never];
  18. export type TransProps<
  19. Key extends ParseKeys<Ns, TOpt, KPrefix>,
  20. Ns extends Namespace = _DefaultNamespace,
  21. KPrefix = undefined,
  22. TContext extends string | undefined = undefined,
  23. TOpt extends TOptions & { context?: TContext } = { context: TContext },
  24. E = React.HTMLProps<HTMLDivElement>,
  25. > = E & {
  26. children?: TransChild | readonly TransChild[];
  27. components?: readonly React.ReactElement[] | { readonly [tagName: string]: React.ReactElement };
  28. count?: number;
  29. context?: TContext;
  30. defaults?: string;
  31. i18n?: i18n;
  32. i18nKey?: Key | Key[];
  33. ns?: Ns;
  34. parent?: string | React.ComponentType<any> | null; // used in React.createElement if not null
  35. tOptions?: TOpt;
  36. values?: {};
  37. shouldUnescape?: boolean;
  38. t?: TFunction<Ns, KPrefix>;
  39. };
  40. export interface TransLegacy {
  41. <
  42. Key extends ParseKeys<Ns, TOpt, KPrefix>,
  43. Ns extends Namespace = _DefaultNamespace,
  44. KPrefix = undefined,
  45. TContext extends string | undefined = undefined,
  46. TOpt extends TOptions & { context?: TContext } = { context: TContext },
  47. E = React.HTMLProps<HTMLDivElement>,
  48. >(
  49. props: TransProps<Key, Ns, KPrefix, TContext, TOpt, E>,
  50. ): React.ReactElement;
  51. }
  52. export interface TransSelectorProps<
  53. Key,
  54. Ns extends Namespace = _DefaultNamespace,
  55. KPrefix = undefined,
  56. TContext extends string | undefined = undefined,
  57. TOpt extends TOptions & { context?: TContext } = { context: TContext },
  58. > {
  59. children?: TransChild | readonly TransChild[];
  60. components?: readonly React.ReactElement[] | { readonly [tagName: string]: React.ReactElement };
  61. count?: number;
  62. context?: TContext;
  63. defaults?: string;
  64. i18n?: i18n;
  65. i18nKey?: Key;
  66. ns?: Ns;
  67. parent?: string | React.ComponentType<any> | null; // used in React.createElement if not null
  68. tOptions?: TOpt;
  69. values?: {};
  70. shouldUnescape?: boolean;
  71. t?: TFunction<Ns, KPrefix>;
  72. }
  73. export interface TransSelector {
  74. <
  75. Target extends ConstrainTarget<TOpt>,
  76. Key extends SelectorFn<GetSource<$NoInfer<Ns>, KPrefix>, ApplyTarget<Target, TOpt>, TOpt>,
  77. const Ns extends Namespace = _DefaultNamespace,
  78. KPrefix = undefined,
  79. TContext extends string | undefined = undefined,
  80. TOpt extends TOptions & { context?: TContext } = { context: TContext },
  81. E = React.HTMLProps<HTMLDivElement>,
  82. >(
  83. props: TransSelectorProps<Key, Ns, KPrefix, TContext, TOpt> & E,
  84. ): React.ReactElement;
  85. }
  86. export const Trans: _EnableSelector extends true | 'optimize' ? TransSelector : TransLegacy;
  87. export type ErrorCode =
  88. | 'NO_I18NEXT_INSTANCE'
  89. | 'NO_LANGUAGES'
  90. | 'DEPRECATED_OPTION'
  91. | 'TRANS_NULL_VALUE'
  92. | 'TRANS_INVALID_OBJ'
  93. | 'TRANS_INVALID_VAR'
  94. | 'TRANS_INVALID_COMPONENTS';
  95. export type ErrorMeta = {
  96. code: ErrorCode;
  97. i18nKey?: string;
  98. [x: string]: any;
  99. };
  100. /**
  101. * Use to type the logger arguments
  102. * @example
  103. * ```
  104. * import type { ErrorArgs } from 'react-i18next';
  105. *
  106. * const logger = {
  107. * // ....
  108. * warn: function (...args: ErrorArgs) {
  109. * if (args[1]?.code === 'TRANS_INVALID_OBJ') {
  110. * const [msg, { i18nKey, ...rest }] = args;
  111. * return log(i18nKey, msg, rest);
  112. * }
  113. * log(...args);
  114. * }
  115. * }
  116. * i18n.use(logger).use(i18nReactPlugin).init({...});
  117. * ```
  118. */
  119. export type ErrorArgs = readonly [string, ErrorMeta | undefined, ...any[]];