Looking to retrieve the types of specific variables within a class. Example:
class CustomPerson {
name: string = "";
age: number = 0;
ID?: number = undefined;
}
getVariableType(CustomPerson, "name") => string
getVariableType(CustomPerson, "age") => number
getVariableType(CustomPerson, "ID") => number | undefined
Prior attempts utilized Object.keys(new CustomPerson())
along with typeof (object as any)[key]
.
However, this method is limited to default variables of new CustomPerson()
, resulting in inability to accurately determine type for ID (returns undefined instead of number | undefined).