When working with TypeScript, it is common to reference interface properties using syntax like Person['gender']
. This allows for clear definition of types in TypeScript, as shown in the following example:
interface Person {
gender: 'male' | 'female';
addresses: {
city: string,
street: string
}[];
}
let person: Person = {
gender: 'male',
addresses: [],
};
let gender: Person['gender'] = person.gender;
Can we also retrieve the array item type of Person['addresses'] in a similar manner?
Note: While creating an Address interface may seem more appropriate in some cases, referencing types this way can sometimes be more concise and easier to follow.