I've been struggling with a simple problem (but it's already givin' me a headache). I have the code 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();
});
I don't know why but object1 returns for me as 'undefined' and when I highlight 'this.array[0]' it returns a JSon like this
"{'object1' : 'someString', 'object2' : 'someString'}"
I want to access object1 but it returns undefined and I can't do a workaround on the Json because typescript recognize the array object as a Bar.
Anyone knows why this is happening and what can I do to access Bar property properly ?
string object1 = ...is not valid syntax. Perhaps you meantlet object1:string = ...? Also, you instantiateFoobut never assign any values toarraysoarray[0]would not exist.