Vue: Prop with defined "type" and "default" set prop to undefined when invalid data is passed

Created on 30 Jul 2016  ·  2Comments  ·  Source: vuejs/vue

Vue.js version

1.0.26

https://jsbin.com/sozesub/10/edit?html,js,console,output

Create a component with a prop with both a "type" and "default" value.

When an invalid type is passed, undefined is set for value rather than the default.

Feels like props should remain with default values when improper data is passed when they are "type" restricted.

Most helpful comment

I would say that this behaviour is intentional and rightly so.

While it may seem to you, the developer, be practical to prevent errors from invalid types by overriding them with a default, it will/can lead to stange behaviour for users - "the app does not show any errors, but somehow the value X simply changed" etc..

If it's undefined, you have the possibility (and responsibility) to handle the error in the component explicitly (_'oops, something went wrong'_) with a simple if (this.prop === undefined)

Does that make sense to you?

All 2 comments

I would say that this behaviour is intentional and rightly so.

While it may seem to you, the developer, be practical to prevent errors from invalid types by overriding them with a default, it will/can lead to stange behaviour for users - "the app does not show any errors, but somehow the value X simply changed" etc..

If it's undefined, you have the possibility (and responsibility) to handle the error in the component explicitly (_'oops, something went wrong'_) with a simple if (this.prop === undefined)

Does that make sense to you?

Feels like this should be denoted in docs as undefined here is really of an error. I swapped back to debugging version and error was shown in console for invalid props.

Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

guan6 picture guan6  ·  3Comments

julianxhokaxhiu picture julianxhokaxhiu  ·  3Comments

gkiely picture gkiely  ·  3Comments

franciscolourenco picture franciscolourenco  ·  3Comments

robertleeplummerjr picture robertleeplummerjr  ·  3Comments