G'ofur N
G'ofur N

Reputation: 2651

VueJs how to set prop type to any?

I have props with defined types but I want the last one of them to accept any kind of value

props: {
        Size: String,
        Label: String,
        Name: String,
        value: Any
    }

Hpw can I achieve this?

Upvotes: 50

Views: 44923

Answers (3)

user7781483
user7781483

Reputation:

From VueJS docs:

null and undefined values will pass any type validation

Or you can use array and place in it all required types:

propB: [String, Number]

Upvotes: 94

The.Bear
The.Bear

Reputation: 5855

To support props type any when you have props with defined types:

props: {
    custom1: null, // set as `null`
    custom2: undefined,  // or `undefined`
       
    // if you need to add extra rules, simply don't set the `type` prop
    custom3: {
        required: true,
        //... rest of your props
    }
}

DEMO: https://jsfiddle.net/xa91zoyq/4/

Upvotes: 4

James Harrington
James Harrington

Reputation: 3226

Here is what I'm doing to pass lint validation.
You could of corse do real validation here.
There are many handy methods that lodash provides for checking types

value: {
  required: true,
  validator: v => true
},

Upvotes: 0

Related Questions