From bf4d621eb89bd71d37a30b0b8fb92f083fd9d659 Mon Sep 17 00:00:00 2001 From: Jackson Date: Thu, 1 Feb 2024 15:03:15 +1100 Subject: [PATCH 1/3] Added withTypes documentation and re-enabled table of contents for this doc --- docs/api/createSelector.mdx | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/docs/api/createSelector.mdx b/docs/api/createSelector.mdx index f18410dae8..f0a7f64728 100644 --- a/docs/api/createSelector.mdx +++ b/docs/api/createSelector.mdx @@ -9,6 +9,8 @@ hide_title: true # `createSelector` +## Overview + The `createSelector` utility from the [Reselect library](https://github.com/reduxjs/reselect), re-exported for ease of use. For more details on using `createSelector`, see: @@ -24,7 +26,7 @@ allowed using string keypaths as input selectors. This was removed, as it ultima the string keypaths made static typing for selectors difficult. ::: -# `createDraftSafeSelector` +## `createDraftSafeSelector` In general, we recommend against using selectors inside of reducers: @@ -86,3 +88,14 @@ const draftSafeSelector = createWeakMapDraftSafeSelector( ``` ::: + +### Defining a Pre-Typed `createDraftSelector` + +As of RTK 2.1, you can define a "pre-typed" version of `createDraftSafeSelector` that can have the type for `state` built in. This lets you set up those types once, so you don't have to repeat them each time you call `createDraftSafeSelector`. + +```ts no-transpile +const createTypedDraftSafeSelector = createDraftSafeSelector.withTypes() +``` + +Import and use the pre-typed `createTypedDraftSafeSelector` function, and it will automatically know that the `state` argument is of type `RootState`. + From e1a42226a85f5e25fee1ef9b0a8653dedc35aa9f Mon Sep 17 00:00:00 2001 From: Jackson Date: Sat, 3 Feb 2024 07:20:23 +1100 Subject: [PATCH 2/3] Copied over reselect known limitations --- docs/api/createSelector.mdx | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/docs/api/createSelector.mdx b/docs/api/createSelector.mdx index f0a7f64728..820aefc858 100644 --- a/docs/api/createSelector.mdx +++ b/docs/api/createSelector.mdx @@ -99,3 +99,39 @@ const createTypedDraftSafeSelector = createDraftSafeSelector.withTypes() + +const selectTodoIds = createTypedDraftSafeSelector( + // Type of `state` is set to `RootState`, no need to manually set the type + state => state.todos, + // ❌ Known limitation: Parameter types are not inferred in this scenario + // so you will have to manually annotate them. + (todos: Todo[]) => todos.map(({ id }) => id) +) +``` From 621b53efa053079d200b28f4319aedda4ba386ff Mon Sep 17 00:00:00 2001 From: Jackson Date: Sat, 3 Feb 2024 07:49:02 +1100 Subject: [PATCH 3/3] Added Known Limitations title to warning --- docs/api/createSelector.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api/createSelector.mdx b/docs/api/createSelector.mdx index 820aefc858..b8b2a96c2b 100644 --- a/docs/api/createSelector.mdx +++ b/docs/api/createSelector.mdx @@ -99,7 +99,7 @@ const createTypedDraftSafeSelector = createDraftSafeSelector.withTypes