Description
Type definition mismatch: isMatchWith
customizer function allows truthy/falsy values but TypeScript types restrict to boolean/undefined
Description
There's an inconsistency between Lodash's actual implementation of isMatchWith
and its TypeScript type definitions. The current TypeScript definition restricts the customizer function's return type to boolean | undefined
, but Lodash's own test cases and implementation suggest it actually accepts any value and treats them as truthy/falsy.
TypeScript Definition
The current type definition in @types/lodash
for isMatchWith
customizer:
interface IsMatchWithCustomizer {
(value: any, other: any, indexOrKey: PropertyName, object: object, source: object): boolean | undefined;
}
declare function isMatchWith(
object: object,
source: object,
customizer: IsMatchWithCustomizer
): boolean;
Error Example
When trying to use Lodash's own test utility function with TypeScript:
import { isMatchWith } from 'lodash';
// This is a utility function similar to one used in Lodash's own tests
const stubA = function() {
return 'a'; // Returns a string, not a boolean
};
const object = { a: 1 };
let actual = isMatchWith(object, { a: 1 }, stubA);
This code produces the following TypeScript error:
Type '() => string' is not assignable to parameter of type 'IsMatchWithCustomizer'.
Type 'string' is not assignable to type 'boolean | undefined'.ts(2345)
Actual Behavior
Looking at Lodash's own test cases in v5-wip
branch, it's clear that the implementation is designed to work with any truthy/falsy values, not just booleans. For example, from Lodash's test:
it('should return a boolean value even when `customizer` does not', () => {
const object = { a: 1 };
let actual = isMatchWith(object, { a: 1 }, stubA);
expect(actual).toBe(true); // passed
// More tests...
});
If you don't mind, I'd like to redefine that type. If possible, please let me know your contribution guide!