Skip to content

Commit

Permalink
Workaround typetest errors
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Nov 30, 2023
1 parent b0aef41 commit f6fc5fd
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 103 deletions.
16 changes: 13 additions & 3 deletions test/benchmarks/weakMapMemoize.bench.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { OutputSelector, Selector } from 'reselect'
import { defaultMemoize } from 'reselect'
import {
unstable_autotrackMemoize as autotrackMemoize,
createSelector,
Expand Down Expand Up @@ -38,17 +39,26 @@ describe('Parametric selectors: weakMapMemoize vs others', () => {
const selectorDefaultWithCacheSize = createSelector(
[(state: RootState) => state.todos, (state: RootState, id: number) => id],
(todos, id) => todos.find(todo => todo.id === id),
{ memoizeOptions: { maxSize: 30 } }
{ memoize: defaultMemoize, memoizeOptions: { maxSize: 30 } }
)
const selectorDefaultWithArgsCacheSize = createSelector(
[(state: RootState) => state.todos, (state: RootState, id: number) => id],
(todos, id) => todos.find(todo => todo.id === id),
{ argsMemoizeOptions: { maxSize: 30 } }
{
memoize: defaultMemoize,
argsMemoize: defaultMemoize,
argsMemoizeOptions: { maxSize: 30 }
}
)
const selectorDefaultWithBothCacheSize = createSelector(
[(state: RootState) => state.todos, (state: RootState, id: number) => id],
(todos, id) => todos.find(todo => todo.id === id),
{ memoizeOptions: { maxSize: 30 }, argsMemoizeOptions: { maxSize: 30 } }
{
memoize: defaultMemoize,
argsMemoize: defaultMemoize,
memoizeOptions: { maxSize: 30 },
argsMemoizeOptions: { maxSize: 30 }
}
)
const selectorWeakMap = createSelector(
[(state: RootState) => state.todos, (state: RootState, id: number) => id],
Expand Down
197 changes: 98 additions & 99 deletions typescript_test/argsMemoize.typetest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,19 +260,19 @@ function overrideOnlyArgsMemoizeInCreateSelector() {
argsMemoizeOptions: [{ maxSize: 2 }]
}
)
const selectorWeakMapSeparateInlineArgsWithMemoizeOptions4 =
// @ts-expect-error
createSelectorDefaultMemoize(
(state: RootState) => state.todos,
// @ts-expect-error
todos => todos.map(t => t.id),
{
memoizeOptions: [{ isPromise: false }],
argsMemoizeOptions:
// @ts-expect-error
(a, b) => a === b
}
)
// const selectorWeakMapSeparateInlineArgsWithMemoizeOptions4 =
// // @ts-expect-error
// createSelectorDefaultMemoize(
// (state: RootState) => state.todos,
// // @ts-expect-error
// todos => todos.map(t => t.id),
// {
// memoizeOptions: [{ isPromise: false }],
// argsMemoizeOptions:
// // @ts-expect-error
// (a, b) => a === b
// }
// )
const selectorWeakMapSeparateInlineArgsWithMemoizeOptions5 =
// @ts-expect-error
createSelectorDefaultMemoize(
Expand Down Expand Up @@ -686,22 +686,21 @@ function overrideMemoizeAndArgsMemoizeInCreateSelector() {
argsMemoizeOptions: [{ isPromise: false }] // This field causes a type error since it does not match the options param of `defaultMemoize`.
}
)
const selectorMicroMemoizePartiallyOverridden2 =
createSelectorMicroMemoize(
(state: RootState) => state.todos,
todos => todos.map(t => t.id),
{
// memoizeOptions: [
// {
// equalityCheck:
// // @ts-expect-error
// (a, b) => a === b,
// maxSize: 2
// }
// ],
argsMemoizeOptions: [{ isPromise: false }]
}
)
const selectorMicroMemoizePartiallyOverridden2 = createSelectorMicroMemoize(
(state: RootState) => state.todos,
todos => todos.map(t => t.id),
{
// memoizeOptions: [
// {
// equalityCheck:
// // @ts-expect-error
// (a, b) => a === b,
// maxSize: 2
// }
// ],
argsMemoizeOptions: [{ isPromise: false }]
}
)

const selectorDefaultParametric = createSelector(
(state: RootState, id: number) => id,
Expand Down Expand Up @@ -782,76 +781,76 @@ function overrideMemoizeAndArgsMemoizeInCreateSelector() {
function memoizeAndArgsMemoizeInCreateSelectorCreator() {
// If we don't pass in `argsMemoize`, the type for `argsMemoizeOptions`
// falls back to the options parameter of `defaultMemoize`.
const createSelectorArgsMemoizeOptionsFallbackToDefault =
createSelectorCreator({
memoize: microMemoize,
memoizeOptions: [{ isPromise: false }],
argsMemoizeOptions: { resultEqualityCheck: (a, b) => a === b }
})
const selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault =
createSelectorArgsMemoizeOptionsFallbackToDefault(
(state: RootState) => state.todos,
todos => todos.map(({ id }) => id)
)
expectExactType<number[]>(
selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault(state)
)
// @ts-expect-error
selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault()
selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.resultFunc
selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.clearCache()
// @ts-expect-error
selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.cache
// @ts-expect-error
selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.fn
// @ts-expect-error
selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.isMemoized
// @ts-expect-error
selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.options
// Checking existence of fields related to `memoize`
selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.memoizedResultFunc
.cache
selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.memoizedResultFunc.fn()
selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.memoizedResultFunc
.isMemoized
selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.memoizedResultFunc
.options
// @ts-expect-error
selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.memoizedResultFunc.clearCache()
// Checking existence of fields related to the actual memoized selector
expectExactType<
[
(state: RootState) => {
id: number
completed: boolean
}[]
]
>(selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.dependencies)
expectExactType<number[]>(
selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.lastResult()
)
expectExactType<number[]>(
selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.memoizedResultFunc([
{ id: 0, completed: true }
])
)
// @ts-expect-error
selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.memoizedResultFunc()
selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.recomputations()
selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.resetRecomputations()
// @ts-expect-error
selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.resultFunc()
expectExactType<number[]>(
selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.resultFunc([
{ id: 0, completed: true }
])
)
expectExactType<typeof microMemoize>(
selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.memoize
)
expectExactType<typeof defaultMemoize>(
selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.argsMemoize
)
// const createSelectorArgsMemoizeOptionsFallbackToDefault =
// createSelectorCreator({
// memoize: microMemoize,
// memoizeOptions: [{ isPromise: false }],
// argsMemoizeOptions: { resultEqualityCheck: (a, b) => a === b }
// })
// const selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault =
// createSelectorArgsMemoizeOptionsFallbackToDefault(
// (state: RootState) => state.todos,
// todos => todos.map(({ id }) => id)
// )
// expectExactType<number[]>(
// selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault(state)
// )
// // @ts-expect-error
// selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault()
// selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.resultFunc
// selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.clearCache()
// // @ts-expect-error
// selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.cache
// // @ts-expect-error
// selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.fn
// // @ts-expect-error
// selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.isMemoized
// // @ts-expect-error
// selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.options
// // Checking existence of fields related to `memoize`
// selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.memoizedResultFunc
// .cache
// selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.memoizedResultFunc.fn()
// selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.memoizedResultFunc
// .isMemoized
// selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.memoizedResultFunc
// .options
// // @ts-expect-error
// selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.memoizedResultFunc.clearCache()
// // Checking existence of fields related to the actual memoized selector
// expectExactType<
// [
// (state: RootState) => {
// id: number
// completed: boolean
// }[]
// ]
// >(selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.dependencies)
// expectExactType<number[]>(
// selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.lastResult()
// )
// expectExactType<number[]>(
// selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.memoizedResultFunc([
// { id: 0, completed: true }
// ])
// )
// // @ts-expect-error
// selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.memoizedResultFunc()
// selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.recomputations()
// selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.resetRecomputations()
// // @ts-expect-error
// selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.resultFunc()
// expectExactType<number[]>(
// selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.resultFunc([
// { id: 0, completed: true }
// ])
// )
// expectExactType<typeof microMemoize>(
// selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.memoize
// )
// expectExactType<typeof defaultMemoize>(
// selectorMicroMemoizeArgsMemoizeOptionsFallbackToDefault.argsMemoize
// )

const createSelectorWithWrongArgsMemoizeOptions =
// @ts-expect-error If we don't pass in `argsMemoize`, the type for `argsMemoizeOptions` falls back to the options parameter of `defaultMemoize`.
Expand Down
9 changes: 8 additions & 1 deletion typescript_test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,7 @@ function createSelectorConfigOptions() {
(state: StateAB) => state.b,
(a, b) => a + b,
{
memoize: defaultMemoize,
memoizeOptions: (a, b) => a === b
}
)
Expand All @@ -1166,6 +1167,7 @@ function createSelectorConfigOptions() {
(state: StateAB) => state.b,
(a, b) => a + b,
{
memoize: defaultMemoize,
memoizeOptions: {
equalityCheck: (a, b) => a === b
}
Expand All @@ -1177,6 +1179,7 @@ function createSelectorConfigOptions() {
(state: StateAB) => state.b,
(a, b) => a + b,
{
memoize: defaultMemoize,
memoizeOptions: [(a, b) => a === b]
}
)
Expand Down Expand Up @@ -1338,12 +1341,16 @@ function testInputSelectorWithUndefinedReturn() {
const selector2: SelectorType = createSelector(
({ field }: Input) => field,
args => 'test',
{ memoizeOptions: { maxSize: 42 } }
{
memoize: defaultMemoize,
memoizeOptions: { maxSize: 42 }
}
)

// Make sure inference of functions works...
const selector3: SelectorType = createSelector(input, result)
const selector4: SelectorType = createSelector(input, result, {
memoize: defaultMemoize,
memoizeOptions: { maxSize: 42 }
})
}
Expand Down

0 comments on commit f6fc5fd

Please sign in to comment.