1

On most components, when using this inside for example a method on my vue object, it gets type CombinedVueInstance, which is how it should be. But sometimes, it gets types like Vue when accessing this in a method and Accessors<DefaultComputed> when accessing this in a computed method, even though nothing seems to be different. This is what the code looks like:

import Vue, { PropType } from 'vue'

export default Vue.extend({
  props: {
    field: Object as PropType<FieldType>,
    row: Boolean as PropType<boolean>,
    events: Object,
  },
  data() {
    return {
      value: undefined,
    }
  },
  computed: {
    required() {
      return this.field.required && !this.value
    },
    invalid() {
      return this.field.isValid && !this.field.isValid(this.value)
    }
  },

Why does this sometimes not get type CombinedVueInstance when used inside the Vue component object?

1 Answer 1

2

Try to give your computed properties explicit return values. (It often removes the error: this not of type CombinedVueInstance in vue with typescript)

computed: {
    required(): boolean {
      return this.field.required && !this.value
    },
    invalid(): boolean {
      return this.field.isValid && !this.field.isValid(this.value)
    }
  },
Sign up to request clarification or add additional context in comments.

8 Comments

Good advice I'm sure but this doesn't answer OP's question at all
Often I get that exact 'error' and it goes away when I add the return values, so, yes, it does answer the question.
Interesting. I can't see how exactly but if this works for OP, fantastic
There is most likely a bug in the parser when it calculates implicit return values. I can state with confidence that adding the return type removes that error many times for me.
Adding explicit return values to the computed properties actually solved it, thank you!
|

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.