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

preserving keys (+ types by key) while mapping over objects (R.mapObjIndexed, R.map) #110

Closed
KiaraGrouwstra opened this issue Nov 20, 2016 · 8 comments

Comments

@KiaraGrouwstra
Copy link
Member

KiaraGrouwstra commented Nov 20, 2016

AFAICT, TS currently doesn't support this. I'd like to leave this as a place-holder for when they would start supporting this though. I think currently this is my main gripe about using Ramda with TS.

Edit: linked issue

@KiaraGrouwstra
Copy link
Member Author

Update: so it looks like it should be possible; thanks to the helpful community at the TypeScript repo for helping point that out.
I'm currently still trying to figure out the details though. Anders was already helpful enough to provide a relevant example in his PR:

function mapObject<K extends string | number, T, U>(obj: Record<K, T>, f: (x: T) => U): Record<K, U>;

My current confusion is w.r.t. how I might be able to reapply constructs like the type T5 = { [P in keyof Item]: Item[P] }; so as to be able to have the result types per key be calculated based on the original type in the object for that value along with the function in/out types. Open to ideas.

@KiaraGrouwstra
Copy link
Member Author

Made a PR for this here now.

@jonaskello
Copy link

jonaskello commented Nov 28, 2016

I think in keyof syntax is new in TS 2.1 so it would be required to run?
For me that is totally OK as I want to add ReadOnlyArray in #113 that is only supported in TS 2.0 :-).

@KiaraGrouwstra
Copy link
Member Author

Yeah, I'm under the impression it's pretty new. That said, I imagine there shouldn't be too many downsides to upgrading...

@blakeembrey
Copy link
Member

This can be supported once 2.2.0 is released as stable. 2.1 is currently in RC, and keyof is currently in the nightlies.

@KiaraGrouwstra
Copy link
Member Author

@blakeembrey: Yeah, basic support (preserving keys) is possible with keyof, though as it stands the full intention here (types separate by key, rather than assuming they're all homogeneous) so far isn't it seems -- note TypeScript's mhegazy couldn't quite figure this out yet either, and eventually recommended following this existing thread.
That said, it definitely seems time to jack up this TS version. :)

@blakeembrey
Copy link
Member

Sounds great. I just found the thread because I was using pick and the latest nightly, and this issue covered the new keyof feature. I'm sure there's some other places where it'll be useful too. Looking forward to 2.2 stable myself, updating the RethinkDB definition will be lots of fun too 😄

@KiaraGrouwstra
Copy link
Member Author

This is mostly blocked by microsoft/TypeScript#6606, will retry after.

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

No branches or pull requests

3 participants