icu.macro.d.ts 3.0 KB

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