question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add warning for illegal initializers for props

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
HerringtonDarkholmecommented, Dec 9, 2016

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.

1reaction
HerringtonDarkholmecommented, Dec 1, 2016

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 with instanceof, that’s one more overhead.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found