Coder Social home page Coder Social logo

.def() doesn't work about vue-types HOT 3 CLOSED

dwightjack avatar dwightjack commented on May 31, 2024
.def() doesn't work

from vue-types.

Comments (3)

dwightjack avatar dwightjack commented on May 31, 2024 1

Yep, more meaningful warnings would be helpful. Opened #13 about it.

Digging in the source, relevant lines about prop validation are here. As far as I can see, default values are used just when prop value is undefined.

To achieve what you're looking for you could use both props and computed:

{
  props: {
    item: VueTypes.shape({ ... })
  },
  computed: {
    mergedItem() {
      return Object.assign({
        theme: '#E58005'
      }, this.item)
    }
  }

}

from vue-types.

dwightjack avatar dwightjack commented on May 31, 2024

Hi,

under the hood the shape type sets a prop validation object like:

{
    type: Object,
    validator() {
      // a custom validator that checks the prop value against the shape
    }
}

The only way to get a default shape is to use the .shape().def() method, but in that case the default shape will just be used when the prop is missing instead of beeing merged with the partial object.

Anyway this behavior matches the Vuejs docs. What's misleading is that the user can invoke .def() without any warning from the library (that's my fault) 😅 .
Looking at vue's source I believe there's no way to get the behavior you're expecting.

from vue-types.

MatteoGabriele avatar MatteoGabriele commented on May 31, 2024

yeah I see what you mean, that would be really powerful though: super useful.

feel free to close the ticket if there's no way to achieve that or at least the warning would be nice :)

speaking about warnings, maybe you prefer a separate ticket, but warning for type checks are not super straight forward in my opinion: what is missing for me is the actual key name.

if i add a prop 'foo' with type string, the console will only complain about a number being passed instead of a string but doesn't specify which key. it's very hard to debug.

Thanks for your time.

from vue-types.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.