Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split out optional last argument (memoizeOptions) into it's own overload #530

Merged
merged 4 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
}
},
{
"files": ["**/test/**/*.ts"],
"files": ["**/test/**/*.ts", "**/typescript_test/**/*.ts"],
"rules": {
"consistent-return": "off",
"max-lines": "off",
Expand All @@ -67,7 +67,8 @@
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/camelcase": "off",
"import/max-dependencies": "off",
"sonarjs/no-duplicate-string": "off"
"sonarjs/no-duplicate-string": "off",
"@typescript-eslint/no-shadow": "off"
}
}
]
Expand Down
33 changes: 23 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export type {
EqualityFn,
SelectorArray,
SelectorResultArray,
ParametricSelector
ParametricSelector,
OutputParametricSelector
} from './types'

import {
Expand Down Expand Up @@ -52,7 +53,7 @@ type DropFirst<T extends unknown[]> = T extends [unknown, ...infer U]
: never

export function createSelectorCreator<
F extends(...args: unknown[]) => unknown,
F extends (...args: unknown[]) => unknown,
MemoizeFunction extends (func: F, ...options: any[]) => F,
MemoizeOptions extends unknown[] = DropFirst<Parameters<MemoizeFunction>>
>(
Expand Down Expand Up @@ -98,7 +99,7 @@ export function createSelectorCreator<
// we wrap it in an array so we can apply it.
const finalMemoizeOptions = Array.isArray(memoizeOptions)
? memoizeOptions
: ([ memoizeOptions ] as MemoizeOptions)
: ([memoizeOptions] as MemoizeOptions)
Copy link
Contributor Author

@eXamadeus eXamadeus Oct 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prettier forced these changes...not sure why they were missing from the main branch...due to not wanting to battle my very opinionated IDE (and turn prettier off), I left them in.


const dependencies = getDependencies(funcs)

Expand Down Expand Up @@ -160,13 +161,25 @@ interface CreateSelectorFunction<
> {
/** Input selectors as separate inline arguments */
<Selectors extends SelectorArray, Result>(
...items:
| [...Selectors, (...args: SelectorResultArray<Selectors>) => Result]
| [
...Selectors,
(...args: SelectorResultArray<Selectors>) => Result,
CreateSelectorOptions<MemoizeOptions>
]
...items: [
...Selectors,
(...args: SelectorResultArray<Selectors>) => Result
]
): OutputSelector<
Selectors,
Result,
GetParamsFromSelectors<Selectors>,
((...args: SelectorResultArray<Selectors>) => Result) &
ReturnType<MemoizeFunction>
>

/** Input selectors as separate inline arguments with memoizeOptions passed */
<Selectors extends SelectorArray, Result>(
...items: [
...Selectors,
(...args: SelectorResultArray<Selectors>) => Result,
CreateSelectorOptions<MemoizeOptions>
]
): OutputSelector<
Selectors,
Result,
Expand Down
34 changes: 32 additions & 2 deletions typescript_test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
createSelectorCreator,
createStructuredSelector,
ParametricSelector,
OutputSelector
OutputSelector,
SelectorResultArray
} from '../src/index'

import microMemoize from 'micro-memoize'
Expand Down Expand Up @@ -162,8 +163,8 @@ function testInvalidTypeInCombinator() {

// does not allow heterogeneous parameter type
// selectors when the combinator function is typed differently
// @ts-expect-error
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error is exactly the same here, just at a higher level. It still provides the same insight.

createSelector(
// @ts-expect-error
(state: { testString: string }) => state.testString,
(state: { testNumber: number }) => state.testNumber,
(state: { testBoolean: boolean }) => state.testBoolean,
Expand Down Expand Up @@ -990,3 +991,32 @@ function createSelectorConfigOptions() {
}
)
}

// Issue #526
function testInputSelectorWithUndefinedReturn() {
type Input = { field: number | undefined }
type Output = string
type SelectorType = (input: Input) => Output

const input = ({ field }: Input) => field
const result = (out: number | undefined): Output => 'test'

// Make sure the selector type is honored
const selector: SelectorType = createSelector(
({ field }: Input) => field,
args => 'test'
)

// even when memoizeOptions are passed
const selector2: SelectorType = createSelector(
({ field }: Input) => field,
args => 'test',
{ memoizeOptions: { maxSize: 42 } }
)

// Make sure inference of functions works...
const selector3: SelectorType = createSelector(input, result)
const selector4: SelectorType = createSelector(input, result, {
memoizeOptions: { maxSize: 42 }
})
Comment on lines +1017 to +1021
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I caught this while trying to make the examples! And the fix also solves this use case.

}