Challenge - 5 Problems
Prop Mastery in Vue
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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>
Attempts:
2 left
💡 Hint
Vue's prop validation warns but does not block rendering.
✗ Incorrect
Vue checks prop types during development and logs warnings in the console if types don't match. It does not stop rendering or convert types automatically.
📝 Syntax
intermediate2: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?Attempts:
2 left
💡 Hint
The validator function receives the prop value and returns true or false.
✗ Incorrect
The correct key is
validator. The function checks val.length >= 3 to allow strings with 3 or more characters. Option C excludes length 3, which is not correct.🔧 Debug
advanced2: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>
Attempts:
2 left
💡 Hint
Vue warnings only appear in development mode, not production.
✗ Incorrect
Vue warns about type mismatches only in development mode. If running in production, warnings are suppressed. The prop type array does not include boolean, so passing boolean should warn in development.
❓ state_output
advanced2: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>
Attempts:
2 left
💡 Hint
Default values are used when no prop is passed.
✗ Incorrect
If the parent does not pass a value, Vue uses the default value specified in the prop declaration.
🧠 Conceptual
expert3: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?Attempts:
2 left
💡 Hint
Think about when TypeScript checks types versus when Vue validates props.
✗ Incorrect
TypeScript types help catch errors before running the app and improve developer experience. Runtime validators check types during execution but add overhead.