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

99
Views
Escriba fuertemente un proxy anidado recursivamente

Tengo algunos objetos en los que almaceno contenido para alguna interfaz de usuario. Se ven así:

 const copy = { header: { content: 'Page Header' }, main: { header: { content: 'content sub header' } body: { content: 'lorem ipsum....' } dynamic: { content: (data: string) => `some ${data}` } } }

He escrito un proxy que me permite obtener una identificación, generada por la ruta profundamente anidada del contenido al que estoy accediendo:

 const isObject = (obj: unknown): obj is Record<string, any> => typeof obj === 'object' && obj != null const createHander = <T extends Record<string, any>>(path: string[]) => ({ get: (target: T, key: string): any => { if (key === 'id') { return path.join('-') } const newTarget = target[key] if (isObject(newTarget)) { return new Proxy(newTarget, createHander([...path, key])) } return newTarget }, }) export const withIds = (copyObject: Record<string, any>, initialId: string) => new Proxy(copyObject, createHander([initialId]))

entonces lo siguiente es cierto:

 const copyWithIds = withIds(copy, 'page') copyWithIds.main.body.id // page-main-body

El problema es que todo en el objeto de copyWithId ahora es del tipo any

Me gustaría no depender de any tipo como el retorno de la get , pero me cuesta encontrar una configuración que funcione.

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

0

Puede usar un tipo asignado para emular lo que está haciendo en el código de función

 type WithId<T> = { [P in keyof T]: T[P] extends Function ? T[P] : T[P] extends object ? WithId<T[P]>: T[P] } & { id: string } export const withIds = <T extends Record<string, any>>(copyObject: T, initialId: string) => new Proxy(copyObject, createHander([initialId])) as unknown as WithId<T>

Enlace del patio de recreo

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!