A TypeScript utility for converting nested JSON objects into TypeScript interfaces, optimized
- Generate TypeScript interfaces from nested JSON objects.
- Support for i18n applications.
- Lightweight and easy to integrate.
Install ts-nester
npm install ts-nester
Or using yarn:
yarn add ts-nester
import { DotNestedKeys } from "ts-nester";
const obj = { foo: { bar: 123, baz: 456 } };
type NestedKeys = DotNestedKeys<typeof obj>; // "foo.bar" | "foo.baz"
import * as en from "en.json";
type TranslationKeys = DotNestedKeys<typeof en>;
// Example of creating a custom hook for translation
export const useAppTranslate = () => {
const { t: tOrigin } = useTranslation();
const t = (key: TranslationKeys) => tOrigin(key);
return {
t,
};
};
export default i18n;
Generates a union of string literals that represent nested keys of an object. Each key is separated by a dot.
T
- The type of the input object.
The union type of keys in the object.
const obj = { foo: { bar: 123, baz: 456 } };
type NestedKeys = DotNestedKeys<typeof obj>; // "foo.bar" | "foo.baz"
Generates a flattened representation of an object's type structure, mapping each nested path to its corresponding value type. This utility type iterates over each key generated by DotNestedKeys<T>
and resolves the type of the value at each nested path. The resulting type is an object with keys in the format 'parent.child', each mapping to the type of the value at that path.
T
- The input object type to be flattened.
const example = { foo: { bar: 123, baz: 456 } };
type Result = FlattenedTypePaths<typeof example>;
// Expected type: {'foo.bar': number, 'foo.baz': number}
Recursively generates a flattened representation of an object's type structure, mapping each nested path to its corresponding value type up to a specified depth. This utility type iterates over each key in the object, and for objects at each level, it recursively applies itself to flatten the structure. The depth parameter controls how deep the type should recurse into the object's structure, allowing for selective depth flattening.
T
- The input object type to be flattened.Depth
- A numeric literal type that specifies the maximum depth to flatten the object. A depth of 1 means no recursion.
const example = {
b: {
c: 1,
d: 2,
e: {
f: 1,
g: {
k: 10,
l: "22",
},
},
},
};
// Flatten up to depth 2
type ResultDepth2 = DeepFlattenedTypePaths<typeof example, 2>;
// Expected type: { 'b.c': number, 'b.d': number, 'b.e': { f: number, g: { k: number, l: string } } }
// Flatten up to depth 3
type ResultDepth3 = DeepFlattenedTypePaths<typeof example, 3>;
// Expected type: { 'b.c': number, 'b.d': number, 'b.e.f': number, 'b.e.g.k': number, 'b.e.g.l': string }
// Example usage
interface Obj {
foo: string[];
bar: { baz: number };
baz: Array<{ id: number; name: string }>;
}
type FooItem = ChildItemType<Obj, "foo">; // string
type BarItem = ChildItemType<Obj, "bar">; // { baz: number }
type BazItem = ChildItemType<Obj, "baz">; // { id: number, name: string }
// Example usage
interface Obj {
foo: string[];
bar: { baz: number };
}
type FooItem = ChildType<Obj, "foo">; // string[]
type BarItem = ChildType<Obj, "bar">; // { baz: number }
Contributions are welcome! If you find any issues or have any suggestions, please open an issue or submit a pull request.
This project is licensed under the ISC License. See the LICENSE file for details.