Vue Tip: Validate Props in Script Setup With TypeScript
You can define prop validations for your component in the <script setup>
using the defineProps()
compiler macro. This is a great way to ensure that the props you receive are of the correct type and shape.
Without TypeScript
Let's first take a look at the official example from the Vue docs without using TypeScript:
With TypeScript
Now let's rewrite the same example using TypeScript inside <script setup>
:
All we need to do is define an interface for the props and use the withDefaults
helper to set default values. TypeScript will take care of the rest.
StackBlitz
Try it yourself on StackBlitz:
If you liked this Vue tip, follow me on X to get notified about new tips, blog posts, and more. Alternatively (or additionally), you can subscribe to my weekly Vue & Nuxt newsletter:
Nuxt Tip: How Nuxt Uses Nitro, h3 and ofetch Internally
Nitro, h3 and ofetch are UnJS packages that are used to power the Nuxt 3 server engine.
Nuxt Tip: Use Environment-Specific Configurations
It sometimes happens that you need to have different configurations for different environments in your Nuxt.js application. Therefore, Nuxt provides fully typed, per-environment overrides for your Nuxt configuration.