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

90
Visualizações
Typescript custom type

If I have an interface in typescript like so

interface MyInterface {
  id: number;
  image: string;
  text: "one thing" | "another" | "anotherthing" | "anotherthing" 
}

How can I factor out my type for text with the pipes so I can use it multiple times as a type in my program? It seems like it should be simple but I'm not sure what the syntax is

For clarity I would like to be able to use it like

factoredOutType = "one thing" | "another" | "anotherthing" | "anotherthing" 

interface MyInterface1 {
  id: number;
  image: string;
  text: factoredOutType
}

interface MyInterface2 {
  name: factoredOutType
}

etc... I can't seem to find the syntax to this question so help is appreciated.

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

0

I think you just need the type keyword:

type factoredOutType = "one thing" | "another" | "anotherthing" | "anotherthing"

Not near a compiler now so can't really check but I think that works

about 4 years ago · Juan Pablo Isaza Relatório

0

you can format string literal types using template literal if your types are related/dynamic. e.g.

type stringTypes =  `${'hello' | 'world'}_id`;
const a: stringTypes = 'hello_id'; 
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