0

I have a simple prop in my Vue.js component in which I want to pass either a boolean or an array.

The problem is that when I do:

props: {
  checked: {
    type: [Boolean, Array],
    default: false,
  },
},

I will have to face the problems an unknow[] array will give me.

And if I do something like this:

props: {
  checked: {
    type: [Boolean, Array as PropType<string[] | number[]>],
    default: false,
  },
},

It will completely break my component. What am I doing wrong?

Thank you in advance for any help my bros.

2
  • 2
    That seems like a Vue bug. A workaround is to do: type: Object as PropType<boolean | string[] | number[]>. That downside to that is you lose runtime prop validation of Boolean during dev, but the TypeScript compiler errors should help. Commented Jul 12, 2022 at 20:27
  • @tony19 That worked. If you take the time to make an answer I'll accept it Commented Jul 19, 2022 at 0:42

2 Answers 2

1

That seems like a Vue bug.

A workaround is to do:

props: {
  checked: {
    type: Object as PropType<boolean | string[] | number[]>,
  },
},

The downside to this is you lose runtime prop validation of Boolean during dev, but the TypeScript compiler would emit an error if you used an invalid type (something other than a boolean, string[], or number[]).

Sign up to request clarification or add additional context in comments.

Comments

0

I think so " Boolean | Array ",It should not be an array

2 Comments

The type field of Vue props supports multiple constructors in an array.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.