Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

155
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda