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

150
Visualizações
Differentiating custom Typescript Interfaces that have the same fields

I have a custom interface like this:

interface Pointer<T> {
  id: string
}

id is a pointer to other data structures, but the id field doesn't contain information about which data structure it is pointing to.

Ideally, I would be able to type the Pointer class as to which data structure the Pointer is pointing to, so that I don't accidentally confuse Pointers. For example:

function getBarPointer(): Pointer<Bar> {
  return { 'id': 'abc123' }
}

function getBazPointer(): Pointer<Baz> {
  return { 'id': 'xyz789' }
}

function useBarPointer(p: Pointer<Bar>): void {
  doSomethingWithP(p)
}

This would allow me to make sure I'm passing in the right Pointers at the type checking stage, while also letting me create functions that operate on specific kinds of pointers.

Is this possible? Or in general is there a way to tell typescript to differentiate types that are ostensibly the same?

Right now, as written, typescript just ignores the passed in type to Pointer.

about 4 years ago · Juan Pablo Isaza
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