In my data structure, I am using generics to build it. However, when I try to populate data, I encounter the need to convert simple formats into the correct types. The issue arises as the class is configured with Generics, making it difficult for me to determine the type of data in other parts of the code.
Below is a snippet extracted from my code:
export class Field<T> {
private Name_: string;
private Value_: T;
constructor(FieldName:string, Data?:T) {
this.Name_ = FieldName;
if( Data !== undefined) {
this.Value_ = Data;
}
}
/**
* Get the data value from this class
* @returns T The value from Value_
*/
get Value(): T {
return this.Value_;
}
get Type(): string {
return typeof this.Value_;
}
/**
* Set the data into the data value
* @param Data T Generic Data member as the raw data to be stored in the field.
*/
set Value(Data:T) {
this.Value_ = Data;
}
}
This example illustrates the challenge I am facing. My data structure (Field) is characterized by a generic (Field), and I anticipate that the data's value should match that type.
I have been attempting to test this but have been unsuccessful in retrieving the type from the Object (as it belongs to the Field type). Therefore, my approach was to ascertain the type of the internal data field.
describe('Get Type of Value', () => {
let TestField:Field<number> = new Field<number>('Age', 32);
it('should return the value type from the definition', () => {
expect(TestField.Type).toBe('number');
});
});
However, the test fails showing that the Type getter function is returning undefined.
FAIL
Expected value to be (using Object.is):
"number"
Received:
"undefined"