Subscribe on changes!

`props.XXX.validator` + `setup()` explode TypeScript compiler.

avatar
Apr 9th 2021

Version

3.0.11

Reproduction link

https://github.com/kuanyui/vue-3-0-11-bug-reproduce-props-and-setup

Steps to reproduce

When props.XXX.validator and setup(props, ctx) are used simultaneously, all props will explode and tsc boom. (though data.XXX are safe.)

Try to remove one of them, compilation will pass.

import { defineComponent } from 'vue';

export default defineComponent({
  props: {
    bystander: { type: String, required: false },  // (Mere an innocent bystander)
    msg: {
      type: String,
      default: '[msg]',
      validator: (x: string) => x.length !== 0   // [[[[[1. Remove this validator
    },
    size: {
      type: Number,
      validator: function (x: number) { return x <= 100 }   // and this validator]]]]]
    },
  },
  data () {
    return {
      numData: 111
    }
  },
  computed: {
    computedNumData (): string { return this.numData + '' },
    computedSize (): string { return this.size + '' },
    computedBystander (): string { return this.bystander + '' },
  },
  methods: {
    onClick () {
      window.alert(this.msg)
      window.alert(this.bystander)
    },
  },
  setup (props, ctx) { return {} }   // [[[[[2. Or remove this setup(props)]]]]]
});

Screenshot_20210409_151615

What is expected?

No explosion.

What is actually happening?

Exploded.


Art is an explosion.

avatar
Apr 9th 2021

You need to use an arrow function or type this as said in the docs:

    size: {
      type: Number,
      validator: function (this: void, x: number) { return x <= 100 }   // and this validator
    },