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

261
Vistas
Property 'firstName' does not exist on type 'object'
function marge(obj1: object, obj2: object) {
    return Object.assign(obj1, obj2);
}

const margeObj = marge( {firstName: "John"}, {lastName : "Doe"} );
console.log(margeObj.firstName);

Typescipt throwing error at last line while trying to get property firstName of mergeObj.

Property 'firstName' does not exist on type 'object'.

Both obj1 and obj2 are object and adding return type object does not fixed this problem.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

TypeScript doesn't understand what type the merge function should return. You can specify the arguments with generics and then merge them in the return type with the & intersection operator:

function marge<T extends {}, S extends {}>(obj1: T, obj2: S): T & S {
    return Object.assign(obj1, obj2);
}

const margeObj = marge({firstName: "John"}, {lastName: "Doe"});
console.log(margeObj.firstName);

The returned type will be the intersection between T and S, all properties in the object arguments will be available in the return type.

X extends {} simply constrains the argument to an object.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I think you've to use an interface like this.

function marge(obj1: object, obj2: object) {
    return Object.assign(obj1, obj2);
}
interface user {
    firstName:string;
    lastName:string;
}
const margeObj:user = marge( {firstName: "John"}, {lastName : "Doe"} );
console.log(margeObj.firstName);
about 4 years ago · Juan Pablo Isaza Denunciar

0

One solution is to use existential quantification, like:

function marge<T, U>(obj1: T, obj2: U): T & U {
  return Object.assign(obj1, obj2);
}

This says, for the params obj1 and obj2 there exists some types, T and U, respectively. The function doesn't know what those types are, but it does know it should return an intersection type from them.

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