Mattias
Mattias

Reputation: 725

this not of type CombinedVueInstance in vue with typescript

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?

Upvotes: 1

Views: 990

Answers (1)

Steven Spungin
Steven Spungin

Reputation: 29119

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)
    }
  },

Upvotes: 2

Related Questions