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

133
Visualizações
TypeScript: how to make a property conditional from an existing type?

So let's say I have

type Link = {
  text: string;
  link: string;
}

interface BigLink extends Link {
  some: number;
  something: string;
  else: string;
}

but I have a variable that shares all these properties except that the link property can be optional. I don't want to create a whole new type just to change one field to link?: string. Is there a way to do something like

type OptionalBigLink = BigLink & { link?: string }

so that I can overwrite the link field and make it optional. ^ The above code still throws an error when I don't pass in the link property.

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Yes you can.

type OptionalBigLink = Partial<Pick<BigLink, "link">> & Omit<BigLink, "link">;

Some tests in the playground

about 4 years ago · Santiago Trujillo Relatório

0

You can use Omit to remove the property from BigLink and then do the intersection.

type OptionalBigLink = Omit<BigLink, "link"> & { link?: string }
about 4 years ago · Santiago Trujillo 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