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

Time:01-27

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

CodePudding user response:

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

type ValueOf<T> = T[keyof T]

Playground Link

  •  Tags:  
  • Related