-
Notifications
You must be signed in to change notification settings - Fork 323
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
feat(differenceBy): add differenceBy
in compat
#487
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
* const result = differenceBy(array1, array2, array3); | ||
* // result will be [1] | ||
*/ | ||
export function differenceBy<T>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For types, we might refer to @types/lodash
.
@types/lodash
provides several overloadings to differenceBy
, could you reference it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@raon0211
Thank you for the review. I plan to add function overloading based on @types/lodash
.
However, I have one question to clarify my understanding. I initially thought that our compatibility functions should have the same types as @types/lodash
to ensure 100% compatibility with Lodash.
However, I noticed that some functions, like difference
, don't exactly match in type.
For example:
@types/lodash
:difference<T>(array: List<T> | null | undefined, ...values: Array<List<T>>): T[];
es-toolkit
:function difference<T>(arr: readonly T[], ...values: Array<readonly T[]>): T[]
This difference has me a bit confused. Could you clarify what needs to be the same and where it's acceptable to have differences?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We initially aimed to keep changes to the original es-toolkit
's signature to a minimum, only referring to types from @types/lodash
to address any type errors in the compatibility tests.
However, I'm wondering whether we should match our types more closely with those from @types/lodash
to make the migration smoother.
For example, readonly T[]
could be updated to readonly T[] | ArrayLike<T>
(which is List<T>
). If we go this route, we might need to add some additional null checks, and wrap arguments with Array.from(...)
to ensure they're converted to arrays where necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We recently launched a Discord channel to help address these types of issues—feel free to join us! :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great if I could work on this after we decide on the direction. I will join the Discord community. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the PR based on @types/lodash
. Please review this when you have some time.
Here’s a key point to note: While @types/lodash
defines the iteratee
parameter using ValueIteratee<T>
, I changed it to ((value: T) => unknown) | PropertyKey
because i think these are the only valid cases supported by Lodash's differenceBy
function. ([PropertyName, any] | PartialShallow<T>
doesn't seem to be valid type)
For reference, here are the type definitions from @types/lodash:
type ValueIteratee<T> = ((value: T) => unknown) | IterateeShorthand<T>;
type IterateeShorthand<T> = PropertyKey | [PropertyKey, any] | PartialShallow<T>;
type PartialShallow<T> = {
[P in keyof T]?: T[P] extends object ? Partial<T[P]> : T[P];
};
resolve #481
Since lodash is testing
difference
,differenceBy
anddifferenceWith
indifference-methods.spec
, i have also includeddifference
test case from ourdifference.spec.ts
.I am not sure about the type of
values
; it would be helpful if you could suggest a more appropriate type if one exists.