destructuring a generic type in order to return an objects property
say i have a function which takes a lambda function as a parameter to access/return properties of an object, is there a way for the generic to be destructured (perhaps recursively?) so i know if that property exists? i would prefer not to extend T to any existing type as i would like to keep this functions input types as general as possible
heres an example for clarification:
const binarySearch = <T, U>(array:T[], value:(T|U), getProperty?:(item:T, index?:number) => U):number => {}
where "U" could be any one of "T"s properties
and which is called either by acessing a property using a lambda if it is an object array
const index = binarySearch(objectArray, objectProperty, (obj) => obj.property)
or by using the array value if it isnt
const index = binarySearch(primitiveArray, primitive)
im expecting the function to behave as a normal binary search method with more versatility as to not have to make several similar functions to access different property types (especially if those properties are nested)
Comments
Post a Comment