I've encountered an issue with a seemingly simple problem that's causing me quite the headache. The code snippet in question is provided below:
interface IFoo{
ReturnFirstBarObject1(): string;
FillBarArray(array: Array<Bar>): void;
}
class Bar {
object1: string;
object2: string;
}
class Foo implements IFoo{
array: Array<Bar>;
constructor(){
this.array = new Array<Bar>();
}
public ReturnFirstBarObject1(): string {
return this.array[0].object1;
}
public FillBarArray(array: Array<Bar>): void {
this.array = array;
}
}
$(document).ready(function(){
var foo = new Foo();
$.get('url/getBars?FooID=1')
.done(function(data: any){
foo.FillBarArray(data);
});
var object1 = foo.ReturnFirstBarObject1();
});
Upon testing, I noticed that when attempting to access 'object1', it returns as 'undefined'. Furthermore, highlighting 'this.array[0]' reveals a JSON string like so:
"{'object1' : 'someString', 'object2' : 'someString'}"
My objective is to properly access the object1 property, but due to TypeScript recognizing the array object as a Bar, I am unable to find a workaround.
If anyone has insights into why this behavior is occurring and suggestions on how I can correctly access the Bar property, your input would be greatly appreciated!