Subscribe on changes!

[defineComponent type error ] when use props validator

avatar
Aug 13th 2021

Version

3.2.2

Reproduction link

https://codesandbox.io/s/using-vuex-4-modules-in-vue-3-with-typescript-forked-1rgil?file=/src/components/HelloWorld.vue

Steps to reproduce

SFC-type-error :

<template>
  {{ direction }}
</template>
<script lang="ts">
import { defineComponent } from "vue";
import type { PropType } from "vue";

type Align = "x" | "y";

export default defineComponent({
  props: {
    direction: {
      type: String as PropType<Align>,
      default: "y",
      validator(direction: Align) {
        return ["x", "y"].includes(direction);
      },
    },
  },
  setup(props) {
  // type error notice here
    console.log(props.direction);
    return {};
  },
});
</script>

SFC -no-type-error:

<template>
  {{ direction }}
</template>
<script lang="ts">
import { defineComponent } from "vue";
import type { PropType } from "vue";

type Align = "x" | "y";

export default defineComponent({
  props: {
    direction: {
      type: String as PropType<Align>,
      default: "y",
      validator:(direction: Align) =>{
        return ["x", "y"].includes(direction);
      },
    },
  },
  setup(props) {
  // type error notice here
    console.log(props.direction);
    return {};
  },
});
</script>

What is expected?

no type error notice

What is actually happening?

type error notice

avatar
Aug 13th 2021

image

avatar
Aug 13th 2021

image works good

avatar
Aug 13th 2021
validator(this:void, direction: Align) {
  return ["x", "y"].includes(direction);
},

see https://v3.vuejs.org/guide/typescript-support.html#annotating-props