0

I have the following class:

class Animal {
  name: string
  age: number
}

I want to be able to retrieve the types of the class's properties without it being initialized. Here is how I expect to be using this:

function getClassPropertyTypes(classType) {
  // perform some magic
}

// The returned value should be either an array or object with the property key and its type
getClassPropertyTypes(Animal)
Yann Thibodeau
  • 1,111
  • 1
  • 12
  • 20
  • It's not clear what you expect as return value, since types are not values. – Andrey Tyukin Nov 14 '20 at 14:50
  • @AndreyTyukin When I say type, I mean the value you get when you do a typeof, does that make it clearer? – Yann Thibodeau Nov 14 '20 at 14:53
  • 1
    If you want union of types of properties, then you can `type PropertyValues = T[keyof T]`, then use it with the class like `PropertyValues`, in this case it will be `string | number`, if this is not what you want, I'm not sure what is. You can't return types from functions, because types don't exist at runtime. – Alex Chashin Nov 14 '20 at 21:13
  • 1
    @AlexChashin Apparently, he wants the values `[ 'string' | 'number' ]` at runtime instead of the type `string | number` at compile time. I am not aware of any functionality within the compiler that would be able to generate different values based on the outcome of type inference. Moreover, a system that would map types to terms would collide with the explicit [non-goal #5 here](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals#non-goals): "[...] emit different code based on the results of the type system". – Andrey Tyukin Nov 14 '20 at 22:22

0 Answers0