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

strong-object

v1.0.1

Published

Strongly typed read-only object methods

Downloads

13

Readme

strong-object

npm version

Overview

strong-object is a TypeScript utility package that provides strongly typed implementations of common Object methods such as Object.entries, Object.keys, and Object.values. These enhanced methods ensure that TypeScript can accurately infer both the keys and values of read-only objects at compile-time, making your object manipulations safer and more predictable.

The package leverages advanced TypeScript type inference, particularly with ReadonlyDeep objects, to guarantee that object interactions remain type-safe, even when working with deeply nested structures.

Features

  • Compile-time type safety for Object.entries, Object.keys, and Object.values.
  • Supports ReadonlyDeep objects, ensuring immutability and type inference for deep structures.
  • Lightweight and designed to integrate seamlessly with TypeScript projects.
  • Provides enhanced guarantees compared to native JavaScript Object methods, which lack compile-time checks.

Installation

To install the package, use npm or yarn:

npm install strong-object

or

yarn add strong-object

Usage

You can import and use the available methods with full TypeScript type inference, ensuring that both keys and values are properly typed.

import { entries, keys, values } from 'strong-object';

const myObject = {
  name: 'Alice',
  age: 30,
  occupation: 'Engineer',
} as const;

// Strongly typed Object.entries
const objectEntries = entries(myObject);
// Inferred type: ReadonlyArray<['name', 'Alice'] | ['age', 30] | ['occupation', 'Engineer']>

// Strongly typed Object.keys
const objectKeys = keys(myObject);
// Inferred type: ReadonlyArray<'name' | 'age' | 'occupation'>

// Strongly typed Object.values
const objectValues = values(myObject);
// Inferred type: ReadonlyArray<'Alice' | 30 | 'Engineer'>

Available Methods

entries

Strongly typed Object.entries for read-only objects. This method returns an array of key-value pairs, where both the keys and values are correctly inferred by TypeScript.

const result = entries({ name: 'Alice', age: 30 } as const);
// Inferred type: ReadonlyArray<['name', 'Alice'] | ['age', 30]>

keys

Strongly typed Object.keys for read-only objects. This method returns an array of the object's keys, with TypeScript ensuring the key types are inferred correctly.

const result = keys({ name: 'Alice', age: 30 } as const);
// Inferred type: ReadonlyArray<'name' | 'age'>

values

Strongly typed Object.values for read-only objects. This method returns an array of the object's values, with TypeScript preserving the specific value types.

const result = values({ name: 'Alice', age: 30 } as const);
// Inferred type: ReadonlyArray<'Alice' | 30>

Type Safety and Compile-Time Guarantees

Unlike the native Object.entries, Object.keys, and Object.values methods, which return general arrays with no specific type information, strong-object provides full compile-time guarantees. Using TypeScript's ReadonlyDeep type from type-fest, this package ensures that even deeply nested object structures retain their immutability and precise type information.

By using strong-object, you can avoid common runtime errors while benefiting from TypeScript's strong typing system, leading to more reliable and maintainable code.

License

This project is licensed under the MIT License.

Contributing

Contributions are welcome! If you find an issue or have a feature request, feel free to submit a pull request or file an issue on the GitHub repository.