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

137
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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