Skip to content

Introduce pre-typed createSelector via createSelector.withTypes<RootState>() method - #673

Merged
EskiMojo14 merged 17 commits into
reduxjs:masterfrom
aryaemami59:typed-createSelector
Jan 3, 2024
Merged

EskiMojo14 merged 17 commits into
reduxjs:masterfrom
aryaemami59:typed-createSelector

Conversation

@aryaemami59

@aryaemami59 aryaemami59 commented Dec 31, 2023 •

Copy link
Copy Markdown
Member

This PR:

  • Implements the ability to create a pre-typed version of createSelector, analogous to the existing createAsyncThunk.withTypes()
    • Currently the only way to lock down the state type with createSelector is to do what is described here, which is quite tedious.
    • This has the benefit of being more consistent with Redux Toolkit's createAsyncThunk.withTypes and dynamicMiddleware.addMiddleware.withTypes
    • With this if you have, for example a createSelector and createSelectorLru, you can simply do
    const createAppSelector = createSelector.withTypes<RootState>()
    const createAppSelectorLru = createSelectorLru.withTypes<RootState>()
  • Adds createSelector.withTypes.test-d.ts type test file
  • Adds createSelector.withTypes.test.ts to test runtime behavior of createSelector.withTypes
  • Adds JSDocs for createSelector.withTypes
  • Adds createSelector.withTypes to docs with examples
  • Partially resolves Add TypedCreateSelector interface to support creating a pre-typed version of createSelector #500.

@netlify

netlify Bot commented Dec 31, 2023 •

Copy link
Copy Markdown

✅ Deploy Preview for reselect-docs canceled.

Name Link
🔨 Latest commit bf43671
🔍 Latest deploy log https://app.netlify.com/sites/reselect-docs/deploys/6595ab0f96f7d500082da067

@codesandbox-ci

codesandbox-ci Bot commented Dec 31, 2023 •

Copy link
Copy Markdown

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 bf43671:

Sandbox Source
Vanilla Typescript Configuration

@aryaemami59
aryaemami59 marked this pull request as ready for review January 2, 2024 19:02
Comment thread src/createSelectorCreator.ts Resolved
@EskiMojo14
EskiMojo14 merged commit 7f6715a into reduxjs:master Jan 3, 2024
@aryaemami59
aryaemami59 deleted the typed-createSelector branch February 8, 2024 11:39
descope Bot referenced this pull request in descope/descope-js Mar 19, 2024
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [reselect](https://togithub.com/reduxjs/reselect) | dependencies |
minor | [`5.0.1` ->
`5.1.0`](https://renovatebot.com/diffs/npm/reselect/5.0.1/5.1.0) |

---

### Release Notes

<details>
<summary>reduxjs/reselect (reselect)</summary>

###
[`v5.1.0`](https://togithub.com/reduxjs/reselect/releases/tag/v5.1.0)

[Compare
Source](https://togithub.com/reduxjs/reselect/compare/v5.0.1...v5.1.0)

This **minor release**:

- Adds a new `createSelector.withTypes<RootState>()` and
`createStructuredSelector.withTypes<RootState>()` API
- Deprecates the `TypedStructuredSelectorCreator` type introduced in 5.0
- Aims to reduce false positives in `identityFunctionCheck` by only
running if the output selector is passed one argument
- Fixes a bug with `weakMapMemoize`'s `resultEqualityCheck` when used
with a primitive result.

##### `withTypes`

Most commonly, selectors will accept the root state of a Redux store as
their first argument. `withTypes` allows you to specify what that first
argument will be ahead of creating the selector, meaning it doesn't have
to be specified.

```ts
// previously
export const selectPostById = createSelector(
  [
    (state: RootState) => state.posts.entities,
    (state: RootState, id: number) => id,
  ],
  (entities, id) => entities[id],
);
// now
export const createAppSelector = createSelector.withTypes<RootState>();

export const selectPostById = createAppSelector(
  [(state) => state.posts.entities, (state, id: number) => id],
  (entities, id) => entities[id],
);
```

##### Known limitations

Due to a Typescript issue, inference of the output selector's parameters
only works with `withTypes` when using an array of input selectors.

If using the variadic version, you can either wrap your input selectors
in an array instance (as above), or annotate the parameters manually.

```ts
export const createAppSelector = createSelector.withTypes<RootState>();

export const selectPostById = createAppSelector(
  (state) => state.posts.entities, 
  (state, id: number) => id,
  // parameters cannot be inferred, so need annotating
  (entities: Record<number, Post>, id: number) => entities[id],
);
```

##### What's Changed

- Reduce `identityFunctionCheck` false positives by
[@&#8203;Methuselah96](https://togithub.com/Methuselah96) in
[https://github.com/reduxjs/reselect/pull/660](https://togithub.com/reduxjs/reselect/pull/660)
- Fix cut content inside TOC of docs by
[@&#8203;aryaemami59](https://togithub.com/aryaemami59) in
[https://github.com/reduxjs/reselect/pull/664](https://togithub.com/reduxjs/reselect/pull/664)
- Remove redundant Svg requires from components in docs by
[@&#8203;aryaemami59](https://togithub.com/aryaemami59) in
[https://github.com/reduxjs/reselect/pull/665](https://togithub.com/reduxjs/reselect/pull/665)
- Fix `_lastResult.deref` is not a function (it is undefined) in React
Native and Expo applications by
[@&#8203;aryaemami59](https://togithub.com/aryaemami59) in
[https://github.com/reduxjs/reselect/pull/671](https://togithub.com/reduxjs/reselect/pull/671)
- Update getting-started.mdx by
[@&#8203;cchaonie](https://togithub.com/cchaonie) in
[https://github.com/reduxjs/reselect/pull/672](https://togithub.com/reduxjs/reselect/pull/672)
- Update createSelectorCreator.mdx with correct defaults by
[@&#8203;lukeapage](https://togithub.com/lukeapage) in
[https://github.com/reduxjs/reselect/pull/674](https://togithub.com/reduxjs/reselect/pull/674)
- Introduce pre-typed `createSelector` via
`createSelector.withTypes<RootState>()` method by
[@&#8203;aryaemami59](https://togithub.com/aryaemami59) in
[https://github.com/reduxjs/reselect/pull/673](https://togithub.com/reduxjs/reselect/pull/673)
- Bump RTK and React-Redux to latest versions by
[@&#8203;aryaemami59](https://togithub.com/aryaemami59) in
[https://github.com/reduxjs/reselect/pull/676](https://togithub.com/reduxjs/reselect/pull/676)
- add publish job by
[@&#8203;EskiMojo14](https://togithub.com/EskiMojo14) in
[https://github.com/reduxjs/reselect/pull/677](https://togithub.com/reduxjs/reselect/pull/677)
- Wrap up implementation of `TypedStructuredSelectorCreator` by
[@&#8203;aryaemami59](https://togithub.com/aryaemami59) in
[https://github.com/reduxjs/reselect/pull/667](https://togithub.com/reduxjs/reselect/pull/667)
- Introduce pre-typed `createStructuredSelector` via
`createStructuredSelector.ts.withTypes<RootState>()` method by
[@&#8203;aryaemami59](https://togithub.com/aryaemami59) in
[https://github.com/reduxjs/reselect/pull/678](https://togithub.com/reduxjs/reselect/pull/678)
- Bump `vitest` to v1 by
[@&#8203;aryaemami59](https://togithub.com/aryaemami59) in
[https://github.com/reduxjs/reselect/pull/668](https://togithub.com/reduxjs/reselect/pull/668)

##### New Contributors

- [@&#8203;Methuselah96](https://togithub.com/Methuselah96) made their
first contribution in
[https://github.com/reduxjs/reselect/pull/660](https://togithub.com/reduxjs/reselect/pull/660)
- [@&#8203;cchaonie](https://togithub.com/cchaonie) made their first
contribution in
[https://github.com/reduxjs/reselect/pull/672](https://togithub.com/reduxjs/reselect/pull/672)

**Full Changelog**:
reduxjs/reselect@v5.0.1...v5.1.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Renovate
Bot](https://togithub.com/renovatebot/renovate).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4xNzAuMCIsInVwZGF0ZWRJblZlciI6IjM3LjE3MC4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->

Co-authored-by: descope[bot] <descope[bot]@users.noreply.github.com>
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