Subscribe on changes!

Vue Typescript PropType not validating props

avatar
Apr 28th 2021

Version

3.0.11

Reproduction link

https://codesandbox.io/s/vue3-ts-proptype-ejt5f

Steps to reproduce

Open the sandbox, see no warning in the console about the msg prop

What is expected?

The prop msg is defined as String as PropType<'test' | 'trolley'>.

When we pass a different value than the ones in the enum, vue should display a warning in the console.

What is actually happening?

No console warning displayed.


I tried to make the validation work on vscode with Vetur extension but it was not warning me about invalid props.

While trying to debug it, I saw that vue was not detecting the invalid props either with PropType

avatar
Apr 28th 2021

misunderstood your meaning just now.

Now, I get it. as PropType<'test' | 'trolley'> is just a type , It will disapper after compiling, so it won't affect runtime. If you want a runtime check, you need do something like this

msg: {
      type: String as PropType<'test' | 'trolley'>,
      validate: (msg: unknown) => msg === 'test' || msg === 'trolley',
   },
avatar
Apr 28th 2021

Runtime validation requires validate. IDE validation can be achieved with VS code extensions like Volar and Vetur but it's not something that can be addressed in Vue Core