1.0.2 • Published 7 months ago
@imhonglu/type-object v1.0.2
@imhonglu/type-object
Introduction
- Provides a dependency-free type-safe wrapper for Object API
- Provides types that closely match native behavior
- Supported APIs
Object.keysObject.entriesObject.fromEntriesObject.hasOwn
Table of Contents
Installation
npm install @imhonglu/type-objectUsage
For detailed examples, please refer to the API Reference.
import * as TypeObject from '@imhonglu/type-object';
const data = { a: 1, b: 2, c: 3 };
for (const key of TypeObject.keys(data)) {
// key: "a" | "b" | "c"
console.log(data[key]); // number
}import * as TypeObject from '@imhonglu/type-object';
const string = 'hello';
for (const index of TypeObject.keys(string)) {
// index: number & keyof string
console.log(string[index]); // string
}import * as TypeObject from '@imhonglu/type-object';
const data: unknown = { name: 'John' };
if (TypeObject.hasOwn(data, 'name')) {
// data: unknown & { name: unknown }
console.log(data.name);
}API Reference
- keys -
Object.keyswrapper - entries -
Object.entrieswrapper - fromEntries -
Object.fromEntrieswrapper - hasOwn -
Object.hasOwnwrapper