0

When using Object properties in Vue Single File Components in TypeScript (generated with Vue CLI 3), the type of the prop is not correct, as shown by the minimal example below.

The type of this.product is (() => any) | ComputedOptions<any>. Does anyone know of a solution to this?

<script lang="ts">
import Vue, { PropType } from 'vue';

interface ProductInterface {
  units: number;
}

export default Vue.extend({
  name: 'Product',
  props: {
    product: {
      type: Object as PropType<ProductInterface>,
      required: true,
    },
  },
  computed: {
    unitsString() {
      return `${this.product.units} Units`;
    },
  },
});
</script>
1
  • This should work fine. Please create a small demo for this using codesandbox.io to show the issue happening. Commented Jun 1, 2020 at 6:38

1 Answer 1

0

Did you try to put a type after the computed property like so?

unitsString() :string{
  return ... 
}

Typescript with Vue reacts weirdly with computed properties which are inferred.

Sign up to request clarification or add additional context in comments.

Comments

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.