TypeScript, checking for keys and subkeys and its variants How to reduce code size?

  getAlreadySaved(goal: GoalModel): number {

        if (goal.status &amp;&amp; goal.status.investedAmount &amp;&amp; !goal.status.pendingIncoming &amp;&amp; !goal.status.pendingOutgoing) {
            return goal.status.investedAmount;

        } else if (goal.status &amp;&amp; goal.status.investedAmount &amp;&amp; goal.status.pendingIncoming &amp;&amp; !goal.status.pendingOutgoing) {
            return goal.status.investedAmount + goal.status.pendingIncoming;

        } else if (goal.status &amp;&amp; goal.status.investedAmount &amp;&amp; !goal.status.pendingIncoming &amp;&amp; goal.status.pendingOutgoing) {
            return goal.status.investedAmount - goal.status.pendingOutgoing;

        } else if (goal.status &amp;&amp; goal.status.investedAmount &amp;&amp; goal.status.pendingIncoming &amp;&amp; goal.status.pendingOutgoing) {
            return goal.status.investedAmount + goal.status.pendingIncoming - goal.status.pendingOutgoing;

        } else {
            return 0;
        }

    }
Find invalid cases first then return early. In this case, it looks like the initial goal.status check is primarily for presence check. You can remove that from the entire logic and put it up front, returning 0 early.

Now the whole point of TypeScript is to provide types to your data so you won't be doing unnecessary type checks in code. In the case of pendingIncoming, pendingOutgoing and investedAmount, they're numbers. The type for status should at least define them as number. Without a value, they should at least be initialized to 0. That should remove the unnecessary conditions.

With that, your code is essentially just:

getAlreadySaved(goal: GoalModel): number {
  const status:Status = goal.status;

  if(!status) return 0;

  // If we can assume they're numbers, we can safely say we can do math.
  return status.investedAmount + status.pendingIncoming - status.pendingOutgoing;
}