Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

433
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda