Currently, I'm tackling a standard filter function (excuse the technical jargon)
The main goal is to create a matcher function that compares a specified property (propertyName: keyof T)
of an object (item: T)
with a search term (term: string)
.
type Predicate<T> = (item: T) => boolean
type ConditionedPredicate<T, C> = (condition: C) => Predicate<T>
export const matchBy = <T>(propertyName: keyof T): ConditionedPredicate<T, string> => (term) => (item) => {
const propertyValue = item[propertyName] as string
const substringRegex = new RegExp(term, 'i')
return Boolean(propertyValue.match(substringRegex))
}
An issue arises from having to convert propertyValue
to a string.
Inquiry: How can I circumvent this by defining a type for propertyName
that only allows keys in T
which are of type string
?