Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

432
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda