0

I'm quite new to using typescript and I'm wanting to make a simple object that holds data relating to a specific users message logs. Here is the basic structure I have at the minute:

export class UserMessageLog {
    date: string;
    askedBy: string;
    msgLogs: { id: string, detail: string }[];

}

When I try to push an object in the format of { id, detail} to the msgLogs property, I get an error in the console stating the msgLogs is undefined. I'm assuming I need to initialize this property in a constructor perhaps? What's the best way to do that?

2
  • 1
    You can just instantiate it in one line msgLogs: { id: string, detail: string }[] = []; Commented Apr 3, 2019 at 12:06
  • Thank you for the help! Commented Apr 3, 2019 at 12:09

1 Answer 1

1

You have to instantiate an array before you can push anything to it. e.g.

const userMessageLog: UserMessageLog = {};
userMessageLog.msgLogs = [];
userMessageLog.msLogs.push(...);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.