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

142
Vistas
Declare a variable with type of other type's variable type

The title looks complex. Let me try to open the topic. Have a look at a type used:

export interface IUser {
  id: string;
  displayName: string;
  email?: string;
  imageUrl?: string;
  disabled?: boolean;
  since?: Date;
}

There is a piece of the code where I need to declare a new variable - id. I expect this variable to be the type to be the same that IUser has as its id. In this case it would be string, but if the IUser is changed, I'd rather not change the variable type manually, but use something like:

let id:typeOf(IUser.id)

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You need to use square bracket notation:

const id:IUser['id'] = '#1'

In this case, if you change the type of id in the interface, type of id variable will be also changed.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use square brackets to define type.

export interface IUser_1 {
  id: string;
}

const id_1: IUser_1['id'] = 'foo';

export interface IUser_2 {
  id: number;
}

const id_2: IUser_2['id'] = 7;
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