0

Please see this minimum example

If I write my props directly inside the component, everything is fine

<script lang="ts">
import Vue, { PropType } from "vue";
export default Vue.extend({
  props: {
    colorType: {
      default: "primary" as const,
      type: String as PropType<"primary" | "secondary" | "other">,
      validator: (value) => ["primary", "secondary", "other"].includes(value),
    },
  },
});
</script>

However, if I extract the props definition outside, it will break types

enter image description here

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

const colorType = {
  default: "primary" as const,
  type: String as PropType<"primary" | "secondary" | "other">,
  validator: (value) => ["primary", "secondary", "other"].includes(value),
};

export default Vue.extend({
  props: {
    colorType,
  },
});
</script>

How can I share those prop type definition accross Vue components without breaking TypeScript?

1 Answer 1

2
const colorType = {
    default: 'primary' as const,
    type: String,
    validator: (value) => ['primary', 'secondary', 'other'].includes(value),
} as PropValidator<'primary' | 'secondary' | 'other'>;

I think this works

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

2 Comments

Note: import { PropValidator } from 'vue/types/options';
Thank you! That's insane TypeScript skills right there.

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.