Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!