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

160
Visualizações
Reference constants in type definition

Typescript weirdness:

export type MyType = 0 | 1 | 2;

This works. But this does not:

export const ONE = 1;
export const TWO = 2;
export const THREE = 3;
export type MyType = ONE | TWO | THREE;
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

With the second example you are in typespace using a value (the declared consts) to declare a type. you need to use typeof ONE TWO or THREE. In the first example, 1 2 and 3 count as types themselves.

about 4 years ago · Juan Pablo Isaza Relatório

0

The problem is that you are defining MyType as a type and there you're referencing a variable. So what you need to actually do to match types with types is to request the type of your value. You can do that by using the typeof operator like:

export const ONE = 1;
export const TWO = 2;
export const THREE = 3;
export type MyType = typeof ONE | typeof TWO | typeof THREE;

Playground

about 4 years ago · Juan Pablo Isaza Relatório

0

First of all you should use typeof, like mentioned in previous answer.

Second, if I understand correctly, you want to create type containing excatly these 3 given values. To do this, you need to use as const keywords.

For example:

export const A = 1 as const;
export const B = 2 as const;
export type MyType = typeof A | typeof B;

and more usefull example for const variable:

export const status = {
  FIRST: 1,
  SECOND: 2
} as const;

export type StatusType = typeof status[keyof typeof status];

export interface IEntity {
  id: number;
  status: StatusType;
}

// You can use it elsewhere like this:
const A: IEntity = {
  id: 1,
  type: status.FIRST
};
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