I am building a component to hold information about fields for use in a form. I need to hold various data objects to create generic routines for working with the data.
export class DataField<T> {
/**
* Data field name IE: database column name, JSON field, etc.
*/
private Name_: string;
private Size_: number;
/**
* The data type for this field. IE: String, Boolean, etc. Note that this is set internally, and is not set by a passed reference
*/
private Type_: T;
// private Type_: { new(): T ;} <- this has not worked either
/**
* Class constructor
* @param FieldName string Name of the Field
* @param FieldSize number Field Size
*/
constructor(FieldName:string, FieldSize:number) {
this.Name_ = FieldName;
this.Size_ = FieldSize;
}
/**
* Get the data type for the value
* @returns string The TypeOf the value
*/
get Type(): string {
return (typeof this.Type_).toString();
}
}
The problem that I have is that the field Type_ is not initialized to anything, so it is always set as undefined. When I create the DataField with the generic, it might look like:
new DataField<string>('FullName', 32);
The generic type T is now string, but I want to be able to get the Type_ variable set so the call to get Type() will return a string.
get Type()does return a string as it is... doesn't it?Type_is never assigned a value... so how can it be anything butundefined? What do you expect to be the content ofType_?Tas generic type parameter - the answer is you can't, type parameters are erased from generated javascript. See also stackoverflow.com/questions/42823198/… and stackoverflow.com/questions/24469856/…Type_property, then it has no other type thanundefined. I guess I still don't understand what you are trying to do...