-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <[email protected]>
- Loading branch information
1 parent
5cd1926
commit a1d3e7f
Showing
4 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}); |