VueJS Using Prop Type Validation With NULL and 'undefined' Values?

32,215

Solution 1

// Basic type check (null and undefined values will pass any type validation)

This applies only when required: true is NOT set. In your code, you are saying that the prop is required and so Vuejs is showing the warning

Related discussion: https://forum.vuejs.org/t/shouldnt-null-be-accepted-as-prop-value-with-any-type/63887

Solution 2

It's because of required: true

From API docs (more detail)

required: Boolean Defines if the prop is required. In a non-production environment, a console warning will be thrown if this value is truthy and the prop is not passed.

Share:
32,215
ux.engineer
Author by

ux.engineer

Widely-versed Senior/Lead Full-stack Web Application Developer who knows how to design. Crafting Single-Page Web or Hybrid Web Applications & User Interfaces for SME business’, organizations, and startups. Experienced with creating products for the International markets, from brand design to technical implementation, maintenance, and support. With industry experience of working in the fields of finance and accounting, insurance, real estate, healthcare, IoT & sensor measurement data, data processing & visualization, public services, marketing, e-commerce, SaaS-services, industrial & manufacturing, electronics, music & events. Self-taught over the past 22 years, with two college degrees as an Electronics Technician & an Entrepreneur, with high school graduation. Senior or Lead Full-Stack Web Developer · Senior Web/UI/UX Designer · Specialist in Data Visualization

Updated on July 05, 2022

Comments

  • ux.engineer
    ux.engineer almost 2 years

    Even though VueJS 2 official documentation about prop validation is stating (as a code example's comment line):

    // Basic type check (null and undefined values will pass any type validation)

    I'm getting the following error with this code reproduction — why is that?

    [Vue warn]: Invalid prop: type check failed for prop "value". Expected String, Number, Boolean, got Null 
    
    <template>
      <div>
        <h1>{{ title }}:</h1>
        <MyInput :value="null" />
      </div>
    </template>
    
    <script>
    Vue.component('MyInput', Vue.extend({
      props: {
        value: {
          type: [String, Number, Boolean],
          required: true,
        },
      },
      template: `
        <select v-model="value">
          <option value="null">
            null value
          </option>
          <option value="">
            Empty value
          </option>
        </select>`,
    }));
    
    export default {
      data: () => ({
        title: 'VueJS Using Prop Type Validation With NULL and `undefined` Values?'
      }),
    };
    </script>