Vue Typescript PropType not validating props
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
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',
},