2

I have a prop called sortSearch which is a function or callback from parent caller.

type SortSearchInterface = (
 currentSort: string,
 currentSortDir: string
) => void;

export default defineComponent({
  props: {
    //sortSearch: {
    //type: Function as PropType<(currentSort: string, currentSortDir: string)=> void>
    // type: Function as PropType<SortSearchInterface>|undefined
    // }
    sortSearch: Function as PropType<SortSearchInterface>,
  },

When attempting to call this function I get the following error Cannot invoke an object which is possibly 'undefined':

     // Cannot invoke an object which is possibly 'undefined'.Vetur(2722)
      //props.sortSearch(s, state.currentSortDir);

With my brief googling found very limited examples / documentation on this and any help appreciated.

1 Answer 1

3

You need to mark this prop as required:

  props: {
    sortSearch: {
      type: Function as PropType<SortSearchInterface>,
      required: true
    },
  },
Sign up to request clarification or add additional context in comments.

2 Comments

Just to confirm that this has worked, thanks, interesting issue and fix, not quite sure how setting something to required = true translates to possibly undefined. It kind of starts to make questionable sense as in you have to question does it make sense
It makes perfect sense. Vue props without required: true are optional - ie. the value can be undefined...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.