0
0
Vueframework~20 mins

Prop types and validation in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prop Mastery in Vue
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a Vue component receives a prop with the wrong type?
Consider a Vue 3 component that declares a prop with type Number. What will happen if the parent passes a String instead?
Vue
<script setup>
const props = defineProps({
  age: Number
})
</script>
<template>
  <p>Age: {{ age }}</p>
</template>
AVue will automatically convert the string to a number before rendering.
BVue will show a console warning about the type mismatch but still render the component with the passed string value.
CVue will throw a runtime error and stop rendering the component.
DVue will ignore the prop and use a default value of 0.
Attempts:
2 left
💡 Hint
Vue's prop validation warns but does not block rendering.
📝 Syntax
intermediate
2:00remaining
Which prop declaration correctly validates a required string with a custom validator?
You want a prop named username that must be a string, is required, and must be at least 3 characters long. Which option is correct?
Ausername: { type: String, required: true, validator: val => val > 3 }
Busername: { type: String, required: true, validate: val => val.length >= 3 }
Cusername: { type: String, required: true, validator: val => val.length >= 3 }
Dusername: { type: String, required: true, validator: val => val.length > 3 }
Attempts:
2 left
💡 Hint
The validator function receives the prop value and returns true or false.
🔧 Debug
advanced
2:00remaining
Why does this prop validation not warn when passing a number?
This Vue component declares a prop with type [String, Number]. But when passing a boolean, no warning appears. Why?
Vue
<script setup>
const props = defineProps({
  value: {
    type: [String, Number],
    required: true
  }
})
</script>
AVue does not warn because the prop type array allows multiple types, but boolean is not one of them, so it should warn but the warning is suppressed in production mode.
BVue does not warn because the prop is required, so any value is accepted without warning.
CVue only warns for types declared in the array; boolean is not included, so it should warn but doesn't due to a bug.
DVue does not warn because the prop type array only checks the first type, ignoring others.
Attempts:
2 left
💡 Hint
Vue warnings only appear in development mode, not production.
state_output
advanced
2:00remaining
What is the value of props.count after mounting?
Given this Vue component, what will props.count be if the parent passes no value?
Vue
<script setup>
const props = defineProps({
  count: {
    type: Number,
    default: 10
  }
})
</script>
<template>
  <p>{{ count }}</p>
</template>
Aundefined
B0
Cnull
D10
Attempts:
2 left
💡 Hint
Default values are used when no prop is passed.
🧠 Conceptual
expert
3:00remaining
Why use defineProps with TypeScript types instead of runtime validators in Vue 3?
In Vue 3 with TypeScript, what is the main advantage of using defineProps<Type>() over runtime prop validation objects?
ATypeScript types provide compile-time checking and better editor support without runtime overhead.
BRuntime validators are faster and more reliable than TypeScript types.
CUsing <code>defineProps&lt;Type&gt;()</code> disables all runtime prop validation.
DTypeScript types automatically convert prop values to the correct type at runtime.
Attempts:
2 left
💡 Hint
Think about when TypeScript checks types versus when Vue validates props.