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

197
Views
¿Cómo escribir un objeto que hace referencia a sí mismo pero que también permite otros valores?

Quiero escribir el siguiente objeto anidado. el tipo funciona cuando lo asigna a un objeto, pero ¿hay alguna forma de hacerlo funcionar al acceder a sus propiedades?

 type NestedObject = { [key: string]: NestedObject | string; } const obj: NestedObject = { a: '123', b: { b_a: { b_a_a: '456' }, b_b: '789' } }; function ABC(myNestedObject: NestedObject) { const nestedObject: NestedObject = { // Property 'fail' does not exist on type 'string | NestedObject'. 'failProperty': myNestedObject.properties.fail }; }

Traté de hacerlo condicional comprobando si se extiende desde la cadena, pero tengo problemas para acceder al valor actual de la clave especificada. Esto, por ejemplo, no funciona:

 type NestedObject = { [K in NestedObject]: NestedObject[K] extends string ? string : NestedObject; }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Debe restringir si la propiedad .properties es una cadena o un NestedObject. Si se trata de una cadena, hacer referencia a myNestedObject.properties.fail generaría problemas, razón por la cual TypeScript le advierte al respecto.

Construya las rutas de código para que si typeof da como resultado string , no haga referencia a myNestedObject.properties.fail .

 function ABC(myNestedObject: NestedObject) { if (typeof myNestedObject.properties === 'string') { throw new Error(); } const nestedObject: NestedObject = { // Property 'fail' does not exist on type 'string | NestedObject'. 'failProperty': myNestedObject.properties.fail }; }
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!