Skip to content

Commit

Permalink
Add objectFromEntries() (#25)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
younho9 and sindresorhus authored Dec 23, 2021
1 parent 5cd1926 commit a1d3e7f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {isDefined} from 'ts-extras';
- [`arrayIncludes`](source/array-includes.ts) - An alternative to `Array#includes()` that properly acts as a type guard.
- [`objectKeys`](source/object-keys.ts) - A strongly-typed version of `Object.keys()`.
- [`objectEntries`](source/object-entries.ts) - A strongly-typed version of `Object.entries()`.
- [`objectFromEntries`](source/object-from-entries.ts) - A strongly-typed version of `Object.fromEntries()`.
- [`objectHasOwn`](source/object-has-own.ts) - A strongly-typed version of `Object.hasOwn()`.
- [`isFinite`](source/is-finite.ts) - A strongly-typed version of `Number.isFinite()`.
- [`isInteger`](source/is-integer.ts) - A strongly-typed version of `Number.isInteger()`.
Expand Down
1 change: 1 addition & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export {isEmpty} from './is-empty.js';
export {asMutable} from './as-mutable.js';
export {arrayIncludes} from './array-includes.js';
export {objectKeys} from './object-keys.js';
export {objectFromEntries} from './object-from-entries.js';
export {objectEntries} from './object-entries.js';
export {objectHasOwn} from './object-has-own.js';
32 changes: 32 additions & 0 deletions source/object-from-entries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
A strongly-typed version of `Object.fromEntries()`.
This is useful since `Object.fromEntries()` always returns `{[key: string]: T}`. This function returns a strongly-typed object from the given array of entries.
- [TypeScript issues about this](https://github.com/microsoft/TypeScript/issues/35745)
@example
```
import {objectFromEntries} from 'ts-extras';
const stronglyTypedObjectFromEntries = objectFromEntries([
['a', 123],
['b', 'someString'],
['c', true],
]);
//=> {a: number; b: string; c: boolean}
const untypedEntries = Object.fromEntries(entries);
//=> {[key: string]: string}
```
@category Improved builtin
@category Type guard
*/
export function objectFromEntries<Key extends PropertyKey, Entries extends Array<[Key, unknown]>>(value: Entries): {
[K in Extract<Entries[number], [Key, unknown]>[0]]: Extract<Entries[number], [K, unknown]>[1]
} {
return Object.fromEntries(value) as {
[K in Extract<Entries[number], [Key, unknown]>[0]]: Extract<Entries[number], [K, unknown]>[1]
};
}
26 changes: 26 additions & 0 deletions test/object-from-entries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import test from 'ava';
import {expectTypeOf} from 'expect-type';
import {objectFromEntries} from '../source/index.js';

test('objectFromEntries()', t => {
type ObjectFromEntries = {
[x: symbol]: boolean;
1: number;
stringKey: string;
};

const symbolKey = Symbol('symbolKey');

const objectFromEntries_ = objectFromEntries([
[1, 123],
['stringKey', 'someString'],
[symbolKey, true],
]);

expectTypeOf<ObjectFromEntries>(objectFromEntries_);
t.deepEqual(objectFromEntries_, {
1: 123,
stringKey: 'someString',
[symbolKey]: true,
});
});

0 comments on commit a1d3e7f

Please sign in to comment.