Skip to content

FahadAminShovon/nested-keys-union

Repository files navigation

nested-keys-union

nested-keys-union is a TypeScript utility type that generates a union type representing all possible nested keys within a given nested object structure. It supports arrays, objects, and primitive types, allowing you to easily navigate and work with deeply nested structures.

Installation

npm install nested-keys-union --save-dev
yarn add -D nested-keys-union
pnpm add nested-keys-union --save-dev

Usage

import { NestedKeys } from 'nested-keys-union';

// Example object with nested structure
const exampleObject = {
  name: 'John Doe',
  age: 30,
  address: {
    city: 'Example City',
    postalCode: '12345',
    contacts: [
      { type: 'email', value: '[email protected]' },
      { type: 'phone', value: '555-1234' },
    ],
  };
};

// Get the union type of all nested keys
type AllNestedKeys = NestedKeys<typeof exampleObject>;

// Usage example
const nestedKey: AllNestedKeys = 'address.contacts.0.value';

In this example, NestedKeys is used to obtain a union type AllNestedKeys representing all possible keys within the exampleObject structure. The resulting type AllNestedKeys can be used to ensure type safety when working with nested keys.

Contribution

Contributions are welcome! Feel free to open issues or submit pull requests to enhance the functionality or documentation.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published