0

You can obtain the type of a property of an object in in typescript as follows:

interface Person {
    name: string;
    realEstate: {
        street: string;
        value: number;
    }[]

}
let mrName: Person['name']

Is there any way to obtain the type of street and value?

1 Answer 1

1

Have you tried this way?

let streetType : Person['realEstate'][0]['street'];

let street : typeof streetType = "Hale";

console.log(street);

let valueType : Person['realEstate'][0]['value'];

let value : typeof valueType = 10000;

console.log(value);

Alternatively, we can define it like below

interface RealEstate {
    street: string;
    value: number;
}

interface Person {
    name: string;
    realEstate: RealEstate[]
}

let realEstateType : RealEstate['street'];

let realEstate : RealEstate = {street: 'Hale', value: 10000};

console.log(realEstate);
Sign up to request clarification or add additional context in comments.

1 Comment

Arbitrarily assigning 0 seems a bit silly, but I guess it works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.