Add warning for illegal initializers for props
See original GitHub issueI have component
import { Vue, Prop, Component } from 'av-ts'
@Component({name: 'best-girl'})
class BestGirl extends Vue {
@Prop name:string = "Asuka"
@Prop isBest:boolean = true
}
I try to use it:
<best-girl @click="hello" girl="Rem" :isBest="false"/>
I get error:
Uncaught TypeError: Cannot create property 'type' on string 'Asuka'
We can trace the error back to prop.ts
:
Component.register(PROP_KEY, function(proto, instance, options) {
let propKeys: string[] = proto[PROP_KEY]
let props = options.props = options.props || createMap()
for (let key of propKeys) {
let prop: PropOptions = {}
if (instance[key] != null) {
prop = instance[key]
delete instance[key]
}
// refill type if not existing, do we need this?
if (!prop.type) { // Error happens here because we can't set .type on numbers, booleans and strings
prop.type = getReflectType(proto, key)
}
props[key] = prop
}
options.props = props
})
I guess I should have wrapped the property in a magic p
function. But that’s not really obvious. Should you not at least provide a warning that we can’t use primitive types like this, or else wrap the value in an object for the user’s sake?
Issue Analytics
- State:
- Created 7 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Failed prop type: Invalid prop `initialValues` supplied to `Form ...
I want to initialize the route param to the input field with hidden type. import React from 'react'; import { Field, reduxForm,propTypes } ......
Read more >Improve warning message for failure to initialize state ... - GitHub
Here's a proposal for a new warning message: MyComponent: Component state must be initialized when using getDerivedStateFromProps. Expected ...
Read more >Object initializer - JavaScript - MDN Web Docs - Mozilla
An object initializer is a comma-delimited list of zero or more pairs of property names and associated values of an object, ...
Read more >Classic Actors - Documentation - Akka
To use Classic Actors, add the following dependency in your project: ... Props object, resulting in an IllegalArgumentException if no or multiple matching ......
Read more >Properties - Manual - PHP
Example #5 Illegal initialization of readonly properties. <?php class Test1 { public readonly string $prop; } $test1 = new Test1; // Illegal initialization...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hmm, it seems hard to do that because my test code already exploit custom
p
function.The ROI is quite low for this functionality, I think.
In general, it is not enough. Because users can pass custom objects to props. To tell a real prop returned by
p
from others we need an additional field, say for example ‘someWeirdPropertyNameOnlyPFunctionReturn’ or return an object we can check withinstanceof
, that’s one more overhead.