[Vuejs]-Returning TypeScript interface member issue

0👍

this.units.find() can return undefined if it doesn’t find a match, which could happen if:

  • this.units is initially empty
  • this.units is not empty, but does not contain an element with an id that matches this.selectedUnitId

You could address this by using the optional chaining operator (and defaulting to an empty string):

export default defineComponent({
  computed: {
    PaymentMessage() {
      const found: UnitFound = this.units.find(/*...*/);
      return found?.payment_message || '';
    }             👆
  },
})

Leave a comment