npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-nester

v1.0.6

Published

A TypeScript utility for converting nested JSON objects into TypeScript interfaces, optimized for i18n applications.

Downloads

53

Readme

ts-nester

ESLint NPM Version

A TypeScript utility for converting nested JSON objects into TypeScript interfaces, optimized

Features

  • Generate TypeScript interfaces from nested JSON objects.
  • Support for i18n applications.
  • Lightweight and easy to integrate.

Installation

Install ts-nester

npm install ts-nester

Or using yarn:

yarn add ts-nester

Usage

Converting Nested Objects to TypeScript Types

import { DotNestedKeys } from "ts-nester";

const obj = { foo: { bar: 123, baz: 456 } };
type NestedKeys = DotNestedKeys<typeof obj>; // "foo.bar" | "foo.baz"

Leveraging for i18n Applications

import * as en from "en.json";

type TranslateKey = 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;

API Reference

DotNestedKeys<T>

Generates a union of string literals that represent nested keys of an object. Each key is separated by a dot.

Type Parameters

  • T - The type of the input object.

Returns

The union type of keys in the object.

Example

const obj = { foo: { bar: 123, baz: 456 } };
type NestedKeys = DotNestedKeys<typeof obj>; // "foo.bar" | "foo.baz"

FlattenedTypePaths<T>

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.

Type Parameters

  • T - The input object type to be flattened.

Example

const example = { foo: { bar: 123, baz: 456 } };
type Result = FlattenedTypePaths<typeof example>;
// Expected type: {'foo.bar': number, 'foo.baz': number}

DeepFlattenedTypePaths<T, Depth>

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.

Type Parameters

  • 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.

Example

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 }

ChildItemType<T, key>

Example

// 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 }

ChildType<T, key>

Example

// Example usage
interface Obj {
  foo: string[];
  bar: { baz: number };
}
type FooItem = ChildType<Obj, "foo">; // string[]
type BarItem = ChildType<Obj, "bar">; // { baz: number }

Contributing

Contributions are welcome! If you find any issues or have any suggestions, please open an issue or submit a pull request.

License

This project is licensed under the ISC License. See the LICENSE file for details.