diff --git a/src/lib/index.ts b/src/lib/index.ts new file mode 100644 index 0000000..fcdac2d --- /dev/null +++ b/src/lib/index.ts @@ -0,0 +1 @@ +export * from './types'; \ No newline at end of file diff --git a/src/lib/types.ts b/src/lib/types.ts new file mode 100644 index 0000000..b244840 --- /dev/null +++ b/src/lib/types.ts @@ -0,0 +1,33 @@ +/** + * @example type Output = Split<'a.b1' | 'a.b2', '.'>; + * // ["a", "b1"] | ["a", "b2"] +*/ +type Split = + S extends `${infer A}${D}${infer B}` ? [A, ...Split] : [S]; + +/** + * @example type Output = NestedObject<["a", "b1"] | ["a", "b2"]>; + * // { a: { b1: string; }; } | { a: { b2: string; }; } +*/ +type NestedObject = + T extends [infer Head, ...infer Tail] ? + Head extends string ? + { [key in Head]: NestedObject } : never : + string; + +/** + * @example type Output = UnionToIntersection<{ a: { b1: string; }; } | { a: { b2: string; }; }>; + * // { a: { b1: string; }; } & { a: { b2: string; }; } +*/ +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; + +/** + * @example type Output = CreateTree<'a.b1' | 'a.b2', '.'>; + * // { a: { b1: string; }; } & { a: { b2: string; }; } +*/ +export type CreateTree = + UnionToIntersection> : never : never>; \ No newline at end of file diff --git a/src/models/i18next.d.ts b/src/models/i18next.d.ts index 7a1a687..7044fc2 100644 --- a/src/models/i18next.d.ts +++ b/src/models/i18next.d.ts @@ -1,24 +1,5 @@ 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; +import { CreateTree } from '../lib'; type LanguageResource = CreateTree; @@ -26,6 +7,6 @@ declare module "i18next" { interface CustomTypeOptions { resources: { '~': LanguageResource - }; + } } } \ No newline at end of file