-
Notifications
You must be signed in to change notification settings - Fork 675
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #673 from aryaemami59/typed-createSelector
Introduce pre-typed `createSelector` via `createSelector.withTypes<RootState>()` method
- Loading branch information
Showing
25 changed files
with
644 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { createSelector } from 'reselect' | ||
|
||
interface Todo { | ||
id: number | ||
completed: boolean | ||
} | ||
|
||
interface Alert { | ||
id: number | ||
read: boolean | ||
} | ||
|
||
export interface RootState { | ||
todos: Todo[] | ||
alerts: Alert[] | ||
} | ||
|
||
export const createAppSelector = createSelector.withTypes<RootState>() | ||
|
||
const selectTodoIds = createAppSelector( | ||
// 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. | ||
// highlight-start | ||
(todos: Todo[]) => todos.map(({ id }) => id) | ||
// highlight-end | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import microMemoize from 'micro-memoize' | ||
import { shallowEqual } from 'react-redux' | ||
import { createSelectorCreator, lruMemoize } from 'reselect' | ||
|
||
export interface RootState { | ||
todos: { id: number; completed: boolean }[] | ||
alerts: { id: number; read: boolean }[] | ||
} | ||
|
||
export const createAppSelector = createSelectorCreator({ | ||
memoize: lruMemoize, | ||
argsMemoize: microMemoize, | ||
memoizeOptions: { | ||
maxSize: 10, | ||
equalityCheck: shallowEqual, | ||
resultEqualityCheck: shallowEqual | ||
}, | ||
argsMemoizeOptions: { | ||
isEqual: shallowEqual, | ||
maxSize: 10 | ||
}, | ||
devModeChecks: { | ||
identityFunctionCheck: 'never', | ||
inputStabilityCheck: 'always' | ||
} | ||
}).withTypes<RootState>() | ||
|
||
const selectReadAlerts = createAppSelector( | ||
[ | ||
// Type of `state` is set to `RootState`, no need to manually set the type | ||
// highlight-start | ||
state => state.alerts | ||
// highlight-end | ||
], | ||
alerts => alerts.filter(({ read }) => read) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { createSelector } from 'reselect' | ||
|
||
export interface RootState { | ||
todos: { id: number; completed: boolean }[] | ||
alerts: { id: number; read: boolean }[] | ||
} | ||
|
||
export const createAppSelector = createSelector.withTypes<RootState>() | ||
|
||
const selectTodoIds = createAppSelector( | ||
[ | ||
// Type of `state` is set to `RootState`, no need to manually set the type | ||
// highlight-start | ||
state => state.todos | ||
// highlight-end | ||
], | ||
todos => todos.map(({ id }) => id) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { createSelector } from 'reselect' | ||
import type { RootState } from './testUtils' | ||
import { localTest } from './testUtils' | ||
|
||
describe(createSelector.withTypes, () => { | ||
const createTypedSelector = createSelector.withTypes<RootState>() | ||
|
||
localTest('should return createSelector', ({ state }) => { | ||
expect(createTypedSelector.withTypes).to.be.a('function') | ||
|
||
expect(createTypedSelector.withTypes().withTypes).to.be.a('function') | ||
|
||
expect(createTypedSelector).toBe(createSelector) | ||
|
||
const selectTodoIds = createTypedSelector([state => state.todos], todos => | ||
todos.map(({ id }) => id) | ||
) | ||
|
||
expect(selectTodoIds).toBeMemoizedSelector() | ||
|
||
expect(selectTodoIds(state)).to.be.an('array').that.is.not.empty | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { Assertion, AsymmetricMatchersContaining } from 'vitest' | ||
|
||
interface CustomMatchers<R = unknown> { | ||
toBeMemoizedSelector(): R | ||
} | ||
|
||
declare module 'vitest' { | ||
interface Assertion<T = any> extends CustomMatchers<T> {} | ||
interface AsymmetricMatchersContaining extends CustomMatchers {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { isMemoizedSelector } from './testUtils' | ||
|
||
expect.extend({ | ||
toBeMemoizedSelector(received) { | ||
const { isNot } = this | ||
|
||
return { | ||
pass: isMemoizedSelector(received), | ||
message: () => `${received} is${isNot ? '' : ' not'} a memoized selector` | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.