0
0
Vueframework~20 mins

Setting up TypeScript in Vue project - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TypeScript Vue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2:00remaining
Correct TypeScript setup in Vue component
Which option shows the correct way to declare a Vue 3 component using TypeScript with the Composition API?
Vue
<script setup lang="ts">
import { ref } from 'vue'

const count = ref(0)
function increment() {
  count.value++
}
</script>

<template>
  <button @click="increment">Count is: {{ count }}</button>
</template>
A
&lt;script lang="ts"&gt;
import { ref } from 'vue'
const count = ref(0)
function increment() {
  count++
}
&lt;/script&gt;
B
&lt;script setup lang="ts"&gt;
import { ref } from 'vue'
const count = ref&lt;number&gt;(0)
function increment() {
  count.value++
}
&lt;/script&gt;
C
&lt;script setup&gt;
import { ref } from 'vue'
const count = ref&lt;number&gt;(0)
function increment() {
  count.value++
}
&lt;/script&gt;
D
&lt;script setup lang="ts"&gt;
import { ref } from 'vue'
const count = ref('0')
function increment() {
  count.value++
}
&lt;/script&gt;
Attempts:
2 left
💡 Hint
Remember to specify lang="ts" and use .value to update refs.
component_behavior
intermediate
1:30remaining
TypeScript prop typing in Vue component
Given this Vue component using TypeScript, what is the type of the prop title inside the component?
Vue
<script setup lang="ts">
import { defineProps } from 'vue'

const props = defineProps<{ title: string }>()
</script>

<template>
  <h1>{{ title }}</h1>
</template>
Astring
Bany
Cnumber
Dundefined
Attempts:
2 left
💡 Hint
Check the type declared inside defineProps.
🔧 Debug
advanced
2:00remaining
Identify the TypeScript error in Vue component
What TypeScript error will this Vue component produce?
Vue
<script setup lang="ts">
import { ref } from 'vue'

const count = ref(0)
function increment() {
  count++
}
</script>
ATS2363: The operand of an increment or decrement operator must be a variable, property or indexer.
BTS2362: The left-hand side of an assignment expression must be a variable or a property access.
CTypeError: count is not a number
DTS2339: Property 'value' does not exist on type 'number'.
Attempts:
2 left
💡 Hint
Remember how to update a ref value in Vue with TypeScript.
🧠 Conceptual
advanced
1:30remaining
Why use lang="ts" in Vue <script>?
What is the main purpose of adding lang="ts" attribute to the <script> tag in a Vue component?
AIt makes the component compatible only with Vue 2 projects.
BIt automatically converts JavaScript code to TypeScript at runtime.
CIt disables JavaScript and only allows TypeScript syntax.
DIt enables TypeScript support and type checking inside the script block.
Attempts:
2 left
💡 Hint
Think about what TypeScript does during development.
state_output
expert
2:00remaining
Output of Vue component with TypeScript reactive state
What will be the rendered output after clicking the button twice in this Vue component?
Vue
<script setup lang="ts">
import { reactive } from 'vue'

const state = reactive({ count: 0 })
function increment() {
  state.count++
}
</script>

<template>
  <button @click="increment">Clicked {{ state.count }} times</button>
</template>
AClicked NaN times
BClicked 0 times
CClicked 2 times
DClicked undefined times
Attempts:
2 left
💡 Hint
Check how reactive state updates and reflects in template.