From 7e18d765bc8180381180a06abce3ff713ad976bc Mon Sep 17 00:00:00 2001 From: RustamRu Date: Sat, 16 Nov 2024 22:41:32 +0300 Subject: [PATCH] feat: init i18n with type hints (#18) --- package-lock.json | 2 ++ package.json | 1 + src/models/i18next.d.ts | 31 +++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 src/models/i18next.d.ts diff --git a/package-lock.json b/package-lock.json index 2ff06ca..6dc5ac0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,6 +21,7 @@ "i18next": "^23.16.4", "react": "^18.3.1", "react-dom": "^18.3.1", + "react-i18next": "^15.1.1", "react-icons": "^5.3.0", "react-router-dom": "^6.27.0" }, @@ -8964,6 +8965,7 @@ "version": "15.1.1", "resolved": "https://registry.npmjs.org/react-i18next/-/react-i18next-15.1.1.tgz", "integrity": "sha512-R/Vg9wIli2P3FfeI8o1eNJUJue5LWpFsQePCHdQDmX0Co3zkr6kdT8gAseb/yGeWbNz1Txc4bKDQuZYsC0kQfw==", + "license": "MIT", "dependencies": { "@babel/runtime": "^7.25.0", "html-parse-stringify": "^3.0.1" diff --git a/package.json b/package.json index 74832ac..8373bb7 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "i18next": "^23.16.4", "react": "^18.3.1", "react-dom": "^18.3.1", + "react-i18next": "^15.1.1", "react-icons": "^5.3.0", "react-router-dom": "^6.27.0" }, diff --git a/src/models/i18next.d.ts b/src/models/i18next.d.ts new file mode 100644 index 0000000..7a1a687 --- /dev/null +++ b/src/models/i18next.d.ts @@ -0,0 +1,31 @@ +import defaultLocale from '../../locales/ru.json'; + +type Split = + S extends `${infer A}${D}${infer B}` ? [A, ...Split] : [S]; + +type NestedObject = + T extends [infer Head, ...infer Tail] ? + Head extends string ? + { [key in Head]: NestedObject } : never : + string; + +// Основная утилита для обработки union type +type CreateTree = + UnionToIntersection> : never : never>; + +// Утилита для объединения типов +type UnionToIntersection = + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never; + +type LanguageResource = CreateTree; + +declare module "i18next" { + interface CustomTypeOptions { + resources: { + '~': LanguageResource + }; + } +} \ No newline at end of file