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)
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.
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;