Categories
TypeScript Answers

How to extract the type of TypeScript interface property?

Spread the love

Sometimes, we want to extract the type of TypeScript interface property.

In this article, we’ll look at how to extract the type of TypeScript interface property.

How to extract the type of TypeScript interface property?

To extract the type of TypeScript interface property, we can use indexed access types.

For instance, we write

interface I1 {
  x: any;
}

interface I2 {
  y: {
    a: I1;
    b: I1;
    c: I1;
  };
  z: any;
}

let x: I2["y"];

to get the type of I2 interface’s y property with I2["y"].

And then we assign the returned type as the type of x.

Conclusion

To extract the type of TypeScript interface property, we can use indexed access types.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *