Let's break down this scenario:
let friends: IUser[];
This line of code declares a variable named friends
with the type IUser[]
, but it is not yet initialized, so its value is currently undefined
.
To initialize it as an empty array, you can do:
let friends: IUser[] = [];
Now that friends
is an empty array, you need to add some content to it. First, create an instance of IUser
:
// Here is one way to create an IUser object
let friend: IUser = {Id: "test", Email: "asdasd"};
Next, push this created user into the array:
friends.push(friend);
Your final code should look like this:
public getFriends(): IUser[] {
let friends: IUser[] = [];
let friend: IUser = {Id: "test", Email: "asdasd"};
friends.push(friend);
return friends;
}