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

Conversation

gibson042
Copy link
Contributor

@gibson042 gibson042 commented Feb 26, 2025

Description

Inspired by Agoric/agoric-sdk#11040 , try to type objectMap more precisely. I'm interested to see what this breaks.

agoric-sdk integration PR: Agoric/agoric-sdk#11064

Copy link
Contributor

@mhofman mhofman left a comment

Choose a reason for hiding this comment

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

It all looks reasonable, but tsc is often unreasonable so without tests I'm not 100% sure it does work.

Edit: oh I see Agoric/agoric-sdk#11064

* 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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants