Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get the type for a value of a property in TS?

I have currently the following code which works, X is a union with all the types defined in MyType, I would like to know if TS has a better way, or an utility fn to get the same result.

type MyType = {
    name :string,
    age: 15
}

type X = MyType[keyof MyType] // string | number

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

There isn’t a predefined one. But you could write your own:

type ValueOf<T> = T[keyof T]

Playground Link

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading