reduxjs / reselect

Selector library for Redux
MIT License
19.04k stars 670 forks source link

Fix hover preview of generated selectors. #633

Closed aryaemami59 closed 1 year ago

aryaemami59 commented 1 year ago

This PR aims to flatten and expand the types for generated selectors in order to improve their visual display inside hover previews.

## This is what it looked like before the changes were made: Inside `typescript_test/test.ts` on line 1269: ![before](https://github.com/reduxjs/reselect/assets/86934138/b10f75ce-6e08-4fb4-8b9d-a80c06f4464b)
## And this is what it looks like after changes were made: Same selector inside `typescript_test/test.ts` now on line 1319: ![after](https://github.com/reduxjs/reselect/assets/86934138/2b0941a0-695d-471b-9ef7-f295310da898)

Other minor changes include:

codesandbox-ci[bot] commented 1 year ago

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 26149932ea9db419c823319b355b06e20e7063f6:

Sandbox Source
Vanilla Configuration
Vanilla Typescript Configuration
aryaemami59 commented 1 year ago

@markerikson This also fixes the TS recursion problem so now we can do this:

deep-nesting

and this:

param-list

markerikson commented 1 year ago

ho. ly. smoke.

that is amazing!

lemme actually read through the changes, but wow, I'm seriously impressed at the apparent results!

aryaemami59 commented 1 year ago

ho. ly. smoke.

that is amazing!

lemme actually read through the changes, but wow, I'm seriously impressed at the apparent results!

Thank you, it feels good to hear that. I don't know if I made it clear enough in the JSDocs and commit messages, but the reason the infnite instantiation bug was happening is because, TypeScript was passing generic parameters into other types that then took those generic parameters and tried to instantiate a type by doing some calculation. The problem was that it was trying to do all of the instantiation using recursion all at the same time which caused the bug in the first place. What I did was that I tried to interrupt the recursion process by expanding and flattening the types (in this case OutputSelector) right before the recursion process begins. So TypeScript now instantiates the type and caches it before recursion so it doesn't have to do all of the work at the same time. I might be completely wrong, but to me it seems like that is what is happening.

markerikson commented 1 year ago

Okay, skimmed through. Obviously that's a pretty big diff, enough so that we're definitely into "LGTM 🤷‍♂️ !" territory :) But the changes that I saw seemed to make sense, and that hover output is exactly what I was hoping to see. Honestly the only tiny half-nitpick I had was that the JSDoc example for createStructuredSelector probably doesn't need to have the actual items arrays filled out, just to save some visual space.

But no reason to hold up this PR for that :)

Meanwhile, totally unrelated to this repo: we're having some active debates about the design of the new createSlice.selectors` field. Latest set of questions is in https://github.com/reduxjs/redux-toolkit/discussions/3387#discussioncomment-7429635 . Would love to have you offer some feedback and help us figure that out so we can finish RTK 2.0!

aryaemami59 commented 1 year ago

Okay, skimmed through. Obviously that's a pretty big diff, enough so that we're definitely into "LGTM 🤷‍♂️ !" territory :) But the changes that I saw seemed to make sense, and that hover output is exactly what I was hoping to see. Honestly the only tiny half-nitpick I had was that the JSDoc example for createStructuredSelector probably doesn't need to have the actual items arrays filled out, just to save some visual space.

But no reason to hold up this PR for that :)

Meanwhile, totally unrelated to this repo: we're having some active debates about the design of the new createSlice.selectors` field. Latest set of questions is in reduxjs/redux-toolkit#3387 (comment) . Would love to have you offer some feedback and help us figure that out so we can finish RTK 2.0!

You're right about the JSDocs, my apologies, I will fix that right away. About the createSlice.selectors, I was actually working with them today, I will join the discussion shortly. And please let me know if there anything else I can help with.