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

TypeScript typings of EqualityFn #73

Closed
SanderDeWaal1992 opened this issue Aug 21, 2019 · 6 comments
Closed

TypeScript typings of EqualityFn #73

SanderDeWaal1992 opened this issue Aug 21, 2019 · 6 comments

Comments

@SanderDeWaal1992
Copy link

SanderDeWaal1992 commented Aug 21, 2019

The arguments of EqualityFn are defined as being unknown even though they are known (i.e. the same as ...newArgs of ResultFn).

Our build did fail today due to the recently added TypeScript types. My suggestion would be to type the args of EqualityFn as any (just like the typings on DefinitelyTyped did which does make that users do not have to cast the unknown type) or to strong-type it, e.g.:

 export type EqualityFn<Args = any> = (
  newArgs: Args,
  lastArgs: Args,
) => boolean;

export default function memoizeOne<
  ResultFn extends (this: any, ...newArgs: T) => ReturnType<ResultFn>, 
  T extends any[]
>(resultFn: ResultFn, isEqual: EqualityFn<T> = areInputsEqual): ResultFn {
...
@alexreardon
Copy link
Owner

I'll get onto this soon

@alexreardon
Copy link
Owner

I suspect this would be sufficient:

type EqualityFn = (newArgs: ReadonlyArray<any>, lastArgs: ReadonlyArray<any>) => boolean;

Do you think the readonly part will cause issues?

@alexreardon
Copy link
Owner

having a strong type on the equality function might be a bit lame for consumers

@SanderDeWaal1992
Copy link
Author

SanderDeWaal1992 commented Aug 22, 2019

I suspect this would be sufficient:

type EqualityFn = (newArgs: ReadonlyArray<any>, lastArgs: ReadonlyArray<any>) => boolean;

Do you think the readonly part will cause issues?

I made an example snippet:

const test = memoizeOne((
	arg1: string
) => {
	return "test" + arg1;
}, (
	a: Readonly<[string]>,
	b: Readonly<[string]>,
) => {
	return someCustomComparison(a as [string], b as [string]);
});

This will result in TS error:

Argument of type '(a: readonly [string], b: readonly [string]) => any' is not assignable to parameter of type 'EqualityFn'.
Types of parameters 'a' and 'newArgs' are incompatible.
Property '0' is missing in type 'readonly any[]' but required in type 'readonly [string]'.ts(2345)

I'm not sure how I -as a user- could type it correctly for TS. I think its obvious for users that the arguments should not be adjusted and that just 'any' should be fine.

@alexreardon
Copy link
Owner

I think this should be fine then:

type EqualityFn = (newArgs: any[], lastArgs: any[]) => boolean;

it is fairly loose, but i don't think the friction is worth typing it stricter for now

@alexreardon
Copy link
Owner

Fixed in 5.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants