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

167
Visualizações
Map a union to a string literal union of the type names

I have a type like this (in this case, the user would be a custom interface):

export type ArgumentTypes = string | number | boolean | user;

now, I would like to have a type of

export type ArgumentTypeNames = "string" | "number" | "boolean" | "user";

I've tried to search around myself for a while, but I have not been able to find a way to get the name. I tried to do ${ArgumentTypes} but that didn't work.

I'm pretty new to advanced types ( Mapped, conditional, and so on ) so I would love an explanation of the answer, or of potential solutions.

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

0

AFAIK, as of today's version of TypeScript, you can't do that without an explicit mapping of string to "string", number to "number" etc. existing somewhere in your program. The mapping might look like this:

interface TypeMapping {
  string: string;
  number: number;
  boolean: boolean;
  user: user;
}

However, not all is lost. You could make this mapping the source of truth, and derive other types from it:

type ArgumentTypeNames = keyof TypeMapping;
type ArgumentTypes = TypeMapping[ArgumentTypeNames];

Try it.

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