Skip to content

Commit

Permalink
Improve selector types
Browse files Browse the repository at this point in the history
  • Loading branch information
thetarnav committed Jun 4, 2023
1 parent 0b80eb7 commit a7270bc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
14 changes: 2 additions & 12 deletions packages/solid/src/reactive/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -784,18 +784,8 @@ export type EqualityCheckerFunction<T, U> = (a: U, b: T) => boolean;
*
* @description https://www.solidjs.com/docs/latest/api#createselector
*/
// default equality checke
export function createSelector<T, U /* U left for backwards compatibility */>(
source: Accessor<T>
): (key: T) => boolean;
// custom equality checker
export function createSelector<T, U /* U left for backwards compatibility */>(
source: Accessor<T>,
fn: EqualityCheckerFunction<T, T>,
options?: BaseOptions
): (key: T) => boolean;
// custom equality checker and key type
export function createSelector<T, U>(
export function createSelector<T, U = T>(source: Accessor<T>): (key: U) => boolean;
export function createSelector<T, U = T>(
source: Accessor<T>,
fn: EqualityCheckerFunction<T, U>,
options?: BaseOptions
Expand Down
22 changes: 16 additions & 6 deletions packages/solid/test/signals.type-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,22 +716,32 @@ const onMemo4: Accessor<number> = onMemo3;
//////////////////////////////////////////////////////////////////////////

{
const source = (): number => 123;
const selector = createSelector(source);
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 source = (): number => 123;
const selector = createSelector(source, (key, source) => key === source);
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 source = (): number => 123;
const selector = createSelector(source, (key: string, source) => Number(key) === source);
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");
Expand Down

0 comments on commit a7270bc

Please sign in to comment.