Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a TypeScript variable in a Vue
Vue
<script setup lang="[1]"> const message: string = 'Hello Vue with TypeScript' </script>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lang="js" instead of lang="ts"
Omitting the lang attribute
Using lang="vue" which is invalid
✗ Incorrect
To use TypeScript in Vue's <script setup> block, set lang="ts".
2fill in blank
mediumComplete the code to import the reactive function from Vue in a TypeScript setup.
Vue
<script setup lang="ts"> import { [1] } from 'vue' const state = reactive({ count: 0 }) </script>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'ref' instead of 'reactive'
Forgetting to import reactive
Using 'watch' when not needed
✗ Incorrect
The reactive function creates a reactive object in Vue.
3fill in blank
hardFix the error in the TypeScript interface declaration for props in a Vue component.
Vue
<script setup lang="ts"> interface Props { title: [1] } const props = defineProps<Props>() </script>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using number instead of string for text props
Using any which loses type safety
Omitting the type causing errors
✗ Incorrect
The title prop should be a string type to hold text.
4fill in blank
hardFill both blanks to correctly define a reactive object with typed properties in Vue with TypeScript.
Vue
<script setup lang="ts"> import { [1] } from 'vue' interface State { count: [2] } const state = reactive<State>({ count: 0 }) </script>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing ref instead of reactive
Using string type for count
Not typing the interface
✗ Incorrect
Use reactive to create a reactive object and number as the type for count.
5fill in blank
hardFill all three blanks to correctly define a computed property with TypeScript in Vue.
Vue
<script setup lang="ts"> import { reactive, [1] } from 'vue' const state = reactive({ count: 1 }) const doubleCount = [2](() => state.count [3] 2) </script>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ref instead of computed
Using ** which is exponentiation, not multiplication
Not importing computed
✗ Incorrect
Use computed to create a computed property and multiply count by 2 using *.