strong-object
v1.0.1
Published
Strongly typed read-only object methods
Downloads
13
Readme
strong-object
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
, andObject.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.