feat: move i18n type utils to lib and describe (#33)

This commit is contained in:
RustamRu
2024-11-17 18:05:24 +03:00
parent 7025c1a31b
commit 823e192c51
3 changed files with 36 additions and 21 deletions

View File

@@ -1,24 +1,5 @@
import defaultLocale from '../../locales/ru.json';
type Split<S extends string, D extends string> =
S extends `${infer A}${D}${infer B}` ? [A, ...Split<B, D>] : [S];
type NestedObject<T extends string[]> =
T extends [infer Head, ...infer Tail] ?
Head extends string ?
{ [key in Head]: NestedObject<Tail extends string[] ? Tail : []> } : never :
string;
// Основная утилита для обработки union type
type CreateTree<T> =
UnionToIntersection<T extends infer U ?
U extends string ?
NestedObject<Split<U, '.'>> : never : never>;
// Утилита для объединения типов
type UnionToIntersection<U> =
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
import { CreateTree } from '../lib';
type LanguageResource = CreateTree<keyof typeof defaultLocale>;
@@ -26,6 +7,6 @@ declare module "i18next" {
interface CustomTypeOptions {
resources: {
'~': LanguageResource
};
}
}
}