-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(common): Improve objectMap typing #2730
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,30 @@ | ||
const { entries, fromEntries } = Object; | ||
/** | ||
* @typedef {<O extends Record<string, unknown>>( | ||
* obj: O, | ||
* ) => { [K in keyof O]: K extends string ? [K, O[K]] : never }[keyof O][]} TypedEntries | ||
*/ | ||
export const typedEntries = /** @type {TypedEntries} */ (Object.entries); | ||
|
||
/** | ||
* @typedef {< | ||
* const Entries extends ReadonlyArray<readonly [PropertyKey, unknown]>, | ||
* >( | ||
* entries: Entries, | ||
* ) => { [Entry in Entries[number] as Entry[0]]: Entry[1] }} FromTypedEntries | ||
*/ | ||
export const fromTypedEntries = /** @type {FromTypedEntries} */ ( | ||
Object.fromEntries | ||
); | ||
|
||
/** | ||
* @typedef {<A extends unknown[], V>( | ||
* arr: A, | ||
* mapper: <K extends number>(el: A[K], idx: K, arr: A) => V, | ||
* ) => V[]} TypedMap | ||
*/ | ||
export const typedMap = /** @type {TypedMap} */ ( | ||
Function.prototype.call.bind(Array.prototype.map) | ||
); | ||
|
||
/** | ||
* By analogy with how `Array.prototype.map` will map the elements of | ||
|
@@ -33,17 +59,18 @@ const { entries, fromEntries } = Object; | |
* if all the mapped values are Passable, then the returned object will be | ||
* a CopyRecord. | ||
* | ||
* @template {Record<string, any>} O | ||
* @template {Record<string, unknown>} O | ||
* @template R map result | ||
* @param {O} original | ||
* @param {(value: O[keyof O], key: keyof O) => R} mapFn | ||
* @returns {Record<keyof O, R>} | ||
* @param {<K extends string & keyof O>(value: O[K], key: K) => R} mapFn | ||
* @returns {{ [K in keyof O]: K extends string ? R : never }} | ||
*/ | ||
export const objectMap = (original, mapFn) => { | ||
const ents = entries(original); | ||
const mapEnts = ents.map( | ||
([k, v]) => /** @type {[keyof O, R]} */ ([k, mapFn(v, k)]), | ||
); | ||
return /** @type {Record<keyof O, R>} */ (harden(fromEntries(mapEnts))); | ||
const oldEntries = typedEntries(original); | ||
/** @type {<K extends string & keyof O>(entry: [K, O[K]]) => [K, R]} */ | ||
const mapEntry = ([k, v]) => [k, mapFn(v, k)]; | ||
const newEntries = typedMap(oldEntries, mapEntry); | ||
const newObj = fromTypedEntries(newEntries); | ||
return /** @type {any} */ (harden(newObj)); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where did it stick here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure; it seems to have just gotten super confused:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like the type mapping failing ... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possibly microsoft/TypeScript#49653 (comment) ? |
||
harden(objectMap); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL Key Remapping via
as