Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

101
Visualizações
Strongly type a recursively nested proxy

I have some objects that I store content for some UI. They look like this:

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

I have written a proxy which lets me get an ID, generated by the deeply nested path of the content I am accessing:

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]))

so the following is true:

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

Trouble is, everything on the copyWithId's object is now of type any

I would like to not be reliant on the any type as the return of the get but struggling to find a set up that works

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use a mapped type to emulate what you are doing in the function code

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>

Playground Link

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda