Type-assert-object is a type assert friendly object utils.
- Static methods of
Object
to generate mapped type- e.g.
entries
,fromEntries
andkeys
- e.g.
- No dependencies
- Simple design
- Light weight
You can install the package from npm.
npm install type-assert-object
or using yarn.
yarn add type-assert-object
type-assert-object's API
import { entries, fromEntries, keys } from 'type-assert-object';
const animals = {
dog: { cry: 'bowwow' },
cat: { cry: 'meow' },
chick: { cry: 'peep' },
} as const;
const animalsArray = entries(animals);
// => (
// ["dog", {readonly cry: "bowwow"; }] |
// ["cat", {readonly cry: "meow" ; }] |
// ["chick", {readonly cry: "peep" ; }] |
// )[]
const animalsObj = fromEntries(animalsArray);
// => {
// dog: {
// readonly cry: "bowwow";
// };
// cat: {
// readonly cry: "meow";
// };
// chick: {
// readonly cry: "peep";
// };
// }
const animalsKey = keys(animals);
// => "dog" | "cat" | "chick"
Normal static method of Object
import { entries, fromEntries, keys } from './index';
const animals = {
dog: { cry: 'bowwow' },
cat: { cry: 'meow' },
chick: { cry: 'peep' },
} as const;
const animalsArray = Object.entries(animals);
// => [string, {readonly cry: "bowwow"; } |
// {readonly cry: "meow" ; } |
// {readonly cry: "peep" ; }
// ][]
const animalsObj = Object.fromEntries(animalsArray);
// => {[k: string], {readonly cry: "bowwow"; } |
// {readonly cry: "meow" ; } |
// {readonly cry: "peep" ; }
// ]
const animalsKey = Object.keys(animals);
// => string[]