I'm trying to replace my Vue 2 options API props object code with Vue 3 script setup syntax in TypeScript.
Existing:
type: {
type: String,
default: 'button',
validator: (prop) => ['button', 'submit', 'reset'].includes(prop)
}
I have this so far:
<script lang="ts" setup>
interface Props {
type?: string;
}
const props = withDefaults(defineProps<Props>(), { type: 'button' });
</script>
But I can't find any info on how to handle prop validators in the script setup syntax.
You can use in defineProps the same structure as in Options API. (DOCS)
<script lang="ts" setup>
const props = defineProps({
type: {
type: String as PropType<'button' | 'submit' | 'reset'>,
default: 'button',
validator: (prop: Type) => ['button', 'submit', 'reset'].includes(prop)
}
});
</script>
I believe I've worked it out, still new to typescript but this should be the equivalent (will be validated in typescript rather than vue runtime)
interface Props {
type?: 'button' | 'submit' | 'reset';
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With