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

Object.fromEntries #78

Open
Jacob-Lockwood opened this issue Feb 24, 2023 · 9 comments
Open

Object.fromEntries #78

Jacob-Lockwood opened this issue Feb 24, 2023 · 9 comments

Comments

@Jacob-Lockwood
Copy link

Jacob-Lockwood commented Feb 24, 2023

Hey, I'm a fan of your YouTube videos and I'm excited to start using ts-reset in my projects.

One feature I would really appreciate would be better typing for the Object constructor method Object.fromEntries:

// currently:
Object.fromEntries([["a", 5], ["b", 6]] as const) // { [k: string]: "a" | "b" }
// ideally:
Object.fromEntries([["a", 5], ["b", 6]] as const) // { a: 5, b: 6 }

I found an implementation at this blog post:

export type ArrayElement<A> = A extends readonly (infer T)[] ? T : never;
type DeepWriteable<T> = { -readonly [P in keyof T]: DeepWriteable<T[P]> };
type Cast<X, Y> = X extends Y ? X : Y
type FromEntries<T> = T extends [infer Key, any][]
  ? { [K in Cast<Key, string>]: Extract<ArrayElement<T>, [K, any]>[1]}
  : { [key in string]: any }
export type FromEntriesWithReadOnly<T> = FromEntries<DeepWriteable<T>>
function fromEntries<T extends Iterable<readonly [PropertyKey, any]>>(entries: T): FromEntriesWithReadOnly<T> {
  return Object.fromEntries(entries) as any
}

Not my code

Here's a playground link for the implementation: TS Playground Link

I'd be glad to write up a PR if you think this is worth adding.

@mattpocock
Copy link
Owner

mattpocock commented Feb 24, 2023

Object.entries won't be added - check the readme!

But Object.fromEntries does seem to make sense - could you reduce the scope of this issue and rewrite the description? Or feel free to create a new issue.

@Jacob-Lockwood Jacob-Lockwood changed the title Object.entries / Object.fromEntries Object.fromEntries Feb 24, 2023
@Jacob-Lockwood
Copy link
Author

@mattpocock Sorry, didn't see that--updated the issue. I can send a PR to add this if you think it would be a good feature

@mattpocock
Copy link
Owner

Let's leave this issue around to see if folks start screaming for it - off the top of my head it feels like a cool feature, but not an essential one.

@akellbl4

This comment was marked as off-topic.

@mattpocock
Copy link
Owner

@akellbl4 Could you create a different issue? As it's unrelated to the current issue.

@akellbl4
Copy link

akellbl4 commented Mar 8, 2023

Moving my example with Object.values and Object.entries inference here since the issue was closed as duplicate

@mattpocock
Copy link
Owner

mattpocock commented Mar 8, 2023

@akellbl4 My bad! Thanks for your patience.

@Briaoeuidhtns
Copy link

I was looking for this also, but that example implementation doesn't seem to work for all cases. Given the input type ['a' | 'b', 1], it gives {a: never; b: never}. I don't know if there exists a right thing to do in that case, since the array may or may not contain all items in the union.

I suppose they could all be marked as possibly undefined instead, but I don't know if that's what we'd want either

@Jacob-Lockwood
Copy link
Author

I was looking for this also, but that example implementation doesn't seem to work for all cases. Given the input type ['a' | 'b', 1], it gives {a: never; b: never}. I don't know if there exists a right thing to do in that case, since the array may or may not contain all items in the union.

I suppose they could all be marked as possibly undefined instead, but I don't know if that's what we'd want either

I think the right way to deal with this would be { a: 1 } | { b : 1 }, but I have no idea how to implement that

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

No branches or pull requests

4 participants