Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions packages/common/object-map.js
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
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Expand Down Expand Up @@ -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));
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did it stick here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure; it seems to have just gotten super confused:

Type '{ [Entry in [keyof O, R] as Entry[0]]: Entry[1]; }' is not assignable to type '{ [K in keyof O]: K extends string ? R : never; }'.
  Type 'keyof O' is not assignable to type '[keyof O, R]'.
    Type 'string | number | symbol' is not assignable to type '[keyof O, R]'.
      Type 'string' is not assignable to type '[keyof O, R]'.(2322)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the type mapping failing ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

harden(objectMap);
Loading