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

105
Views
Interfaz para mapear valores de 2 objetos en Typescript

tengo 2 objetos objA y objB

Tengo una función que acepta el valor de objB y la clave de objA.

 const objA = { a:"a", b:"b" } const objB = { a:"a", b:"b" } checkValues = (type:string,key:keyof typeof objA)=>{ return objA[key] === type; } checkValues("A",objA.a)

Pero obtener el error Argument of type 'string' is not assignable to parameter of type '"a" | "b"'

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

0

Al definir su función de esa manera, solo puede verificar las entries del objeto (por lo tanto, 'a' | 'b'), en cambio, llama a la función pasando objA.a que en cambio se refiere al valor de la propiedad a en el objeto.

Lo que puedes hacer es:

 //Calling the function and passing only 'a' or 'b' checkValues("A", 'a'); checkValues("A", 'b');
 //Declaring object properties with types through the use of interfaces: interface MyObj { a: 'a' | 'b'; b: 'a' | 'b'; } const objA: MyObj = { a:"a", b:"b" } const objB: MyObj = { a:"a", b:"b" }

Realmente no ha especificado lo que necesita, así que esto es lo que probablemente haría, espero que ayude

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!