Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

431
Views
Typescript: Compare two different Array of objects having a common key and get common objects from source array

I am trying to compare if an item exists in another array by checking a common property value. For the below code it is throwing the below error:

TS2367: This condition will always return 'false' since the types 'U[K]' and 'T[K]' have no overlap.

 const sourceArray = [{requestId: "1", name: "henry"},{requestId: "2", name: "bob"}] 
 const arrayListToCompareWith = [{requestId: "1", age: 30}, {requestId: "9", age: 40}]
 const commonItems = getItemsInSourceArrayWhichArePresentInArrayToBeComparedWith(sourceArray,arrayListToCompareWith, "requestId" )
 
 function getItemsInSourceArrayWhichArePresentInArrayToBeComparedWith<
    T,
    U,
    K extends keyof T & keyof U,
  >(
    sourceArray: T[],
    arrayListToCompareWith: U[],
    comparisonKey: K, // "requestId"
  ) {
    const itemsInSourceArrayWhichAreNotPresentInArrayToBeComparedWith: T[] =
      sourceArray.filter((sourceArrayItem: T): boolean => {
        const { [comparisonKey]: sourceArrayItemComparisonKeyValue } = sourceArrayItem; sourceArrayItemComparisonKeyValue
        const itemExistsInTheArrayWeAreComparingWith: boolean =
          arrayListToCompareWith.some((itemToCompareWith: U): boolean => {
            const comparisonValueInItemToCompareWith = itemToCompareWith[comparisonKey]; // U[K]
            const comparisonValueInSource = sourceArrayItemComparisonKeyValue; //T[K]
            // TS2367: This condition will always return 'false' since the types 'U[K]' and 'T[K]' have no overlap.
            const check = comparisonValueInItemToCompareWith === comparisonValueInSource;
            return check;
          });
        return itemExistsInTheArrayWeAreComparingWith;
      });
    return itemsInSourceArrayWhichArePresentInArrayToBeComparedWith;
  }

Below is the TS playground link for the above code problem:

TS Playground link to run the code

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The problem is that you didn't tell Typescript that there is any relationship between T and U, in particular you didn't say that they must have the same value type for the property K.

Actually there is no need for your second type parameter U - all we need to say is that the second array contains objects which have a key K with the same value type as T does. This can be more simply written as Pick<T, K>.

function filterByKey<T, K extends keyof T>(arr: T[], keysToKeep: Pick<T, K>[], key: K) {
  return arr.filter(
    item => keysToKeep.some(
      otherItem => item[key] === otherItem[key]
    )
  );
}

Playground Link

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!