1

I have a Vue component that has a property defined using a decorator:

import { Component, Vue } from "vue-property-decorator"
@Component({
             props: {
               myId: String,
             },
           })
class TestProp extends Vue {
  myFunction(obj: any) {
    return obj[this.myId] // here tslint complains: "Property 'myId' does not exist on type 'TestProp'."
  }
}

I can avoid the type error by converting this to any:

  myFunction(obj: any) {
    return obj[(this as any).myId]
  }

But this is rather a work around than a solution.

I guess compiler isn't aware of the properties defined by the @Component decorator?

Any ideas?

1
  • please share complete error details Commented Jul 26, 2019 at 17:05

2 Answers 2

3

I recommend you to use this library: https://github.com/kaorun343/vue-property-decorator

With this you can declare your prop inside your component class.

For example:

import { Vue, Component, Prop } from 'vue-property-decorator'

@Component
class TestProp extends Vue {
  @Prop(String) myId: string!
}
Sign up to request clarification or add additional context in comments.

2 Comments

I added the other answer, but this is what I was looking for and didn't find. Vue needs to convert to this way instead of a single decorator
This way to declare properties is really nice, thanks!
1

The TypeScript example states that you must document them yourself in the component.

From that page

  // additional declaration is needed
  // when you declare some properties in `Component` decorator
import { Component, Vue } from "vue-property-decorator"
@Component({
  props: {
    myId: String,
  },
})
class TestProp extends Vue {

  myId: string;

  myFunction(obj: any) {
    return obj[this.myId] // here tslint complains: "Property 'myId' does not exist on type 'TestProp'."
  }
}

2 Comments

Thanks for pointing at this example. However, in the example the propMessage isn't declared again in the class ...
It is wrong in that example, it won't compile of you use it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.