Skip to content

Commit

Permalink
Improve type inference of createSelector. (#1758)
Browse files Browse the repository at this point in the history
* Add function overloads to `createSelector` to improve type inference.

* Improve selector types

* Remove unnecessary overload from createSelector
  • Loading branch information
thetarnav committed Jun 5, 2023
1 parent 5e2999e commit 46e5e78
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/five-cats-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"solid-js": patch
---

Improve type inference of `createSelector`.
2 changes: 1 addition & 1 deletion packages/solid/src/reactive/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ export type EqualityCheckerFunction<T, U> = (a: U, b: T) => boolean;
*
* @description https://www.solidjs.com/docs/latest/api#createselector
*/
export function createSelector<T, U>(
export function createSelector<T, U = T>(
source: Accessor<T>,
fn: EqualityCheckerFunction<T, U> = equalFn as TODO,
options?: BaseOptions
Expand Down
53 changes: 53 additions & 0 deletions packages/solid/test/signals.type-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Accessor,
on,
createSignal,
createSelector,
Signal,
Setter
// } from "../types/index";
Expand Down Expand Up @@ -710,6 +711,58 @@ const onMemo3 = createMemo(
// @ts-expect-error when deferred the type includes undefined
const onMemo4: Accessor<number> = onMemo3;

//////////////////////////////////////////////////////////////////////////
// createSelector ////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////

{
const selector = createSelector(() => 123);
const bool: boolean = selector(123);
// @ts-expect-error Argument of type 'string' is not assignable to parameter of type 'number'. ts(2345)
const bool2: boolean = selector("123");
}
{
const selector = createSelector(() => 123, undefined, { name: "test" });
const bool: boolean = selector(123);
// @ts-expect-error Argument of type 'string' is not assignable to parameter of type 'number'. ts(2345)
const bool2: boolean = selector("123");
}
{
const selector = createSelector<number | string>(() => 123);
const bool: boolean = selector(123);
const bool2: boolean = selector("123");
// @ts-expect-error Argument of type 'null' is not assignable to parameter of type 'string | number'. ts(2345)
const bool3: boolean = selector(null);
}
{
const selector = createSelector(
() => 123,
(key, source) => key === source
);
const bool: boolean = selector(123);
// @ts-expect-error Argument of type 'string' is not assignable to parameter of type 'number'. ts(2345)
const bool2: boolean = selector("123");
}
{
const selector = createSelector(
() => 123,
(key: string, source) => Number(key) === source
);
// @ts-expect-error Argument of type 'number' is not assignable to parameter of type 'string'. ts(2345)
const bool: boolean = selector(123);
const bool2: boolean = selector("123");
}
{
const selector = createSelector(
() => 123,
(key, source) => key === source,
{ name: "test" }
);
const bool: boolean = selector(123);
// @ts-expect-error Argument of type 'string' is not assignable to parameter of type 'number'. ts(2345)
const bool2: boolean = selector("123");
}

//////////////////////////////////////////////////////////////////////////
// variations of signal types ////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
Expand Down
6 changes: 5 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 46e5e78

Please sign in to comment.