Bird
Raised Fist0
Vueframework~20 mins

Setting up TypeScript in Vue project - Practice Exercises

Choose your learning style10 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.

Practice

(1/5)
1. What is the main benefit of using TypeScript in a Vue project?
easy
A. It helps catch errors early and improves code clarity.
B. It automatically styles your components.
C. It replaces Vue's template syntax with HTML.
D. It removes the need for JavaScript entirely.

Solution

  1. Step 1: Understand TypeScript's role

    TypeScript adds static typing to JavaScript, helping catch errors before running code.
  2. Step 2: Relate to Vue projects

    Using TypeScript in Vue improves code clarity and reduces runtime bugs.
  3. Final Answer:

    It helps catch errors early and improves code clarity. -> Option A
  4. Quick Check:

    TypeScript improves code safety = A [OK]
Hint: TypeScript catches errors before running code [OK]
Common Mistakes:
  • Thinking TypeScript styles components
  • Confusing TypeScript with HTML templates
  • Believing TypeScript removes JavaScript
2. Which command is used to create a new Vue project with TypeScript support?
easy
A. vue create project --typescript
B. npm create vue@latest and select TypeScript option
C. npm install vue typescript
D. vue add typescript

Solution

  1. Step 1: Identify the official Vue CLI command

    The recommended way is using npm create vue@latest to start a new project.
  2. Step 2: Select TypeScript during setup

    During the interactive setup, you choose TypeScript to enable it.
  3. Final Answer:

    npm create vue@latest and select TypeScript option -> Option B
  4. Quick Check:

    Use npm create vue@latest for new Vue projects [OK]
Hint: Use npm create vue@latest and pick TypeScript [OK]
Common Mistakes:
  • Using old vue CLI commands
  • Trying to install TypeScript separately after project creation
  • Confusing add commands with create commands
3. Given this Vue component script tag, what is missing to enable TypeScript?
<script>
export default {
  data() {
    return { count: 0 }
  }
}
</script>
medium
A. Wrap the export in a defineComponent call
B. Change data() to setup()
C. Add type="module" to the <script> tag
D. Add lang="ts" attribute to the <script> tag

Solution

  1. Step 1: Identify how to enable TypeScript in Vue scripts

    Vue requires lang="ts" on the <script> tag to treat it as TypeScript.
  2. Step 2: Check other options

    Changing to setup() or adding type="module" does not enable TypeScript automatically.
  3. Final Answer:

    Add lang="ts" attribute to the <script> tag -> Option D
  4. Quick Check:

    Use lang="ts" to enable TypeScript [OK]
Hint: Add lang="ts" to script tag for TypeScript [OK]
Common Mistakes:
  • Forgetting lang="ts" on script tag
  • Confusing setup() with TypeScript enabling
  • Assuming type="module" enables TypeScript
4. You added lang="ts" to your Vue component but get errors about missing types. What is the likely cause?
medium
A. You forgot to create or configure tsconfig.json in the project root.
B. You need to remove lang="ts" to fix errors.
C. Vue does not support TypeScript in single file components.
D. You must rename the file extension to .ts instead of .vue.

Solution

  1. Step 1: Understand TypeScript configuration needs

    TypeScript requires a tsconfig.json file to know how to compile and check types.
  2. Step 2: Check other options

    Removing lang="ts" disables TypeScript, Vue supports TypeScript in .vue files, and renaming files is not needed.
  3. Final Answer:

    You forgot to create or configure tsconfig.json in the project root. -> Option A
  4. Quick Check:

    tsconfig.json is required for TypeScript setup [OK]
Hint: Create tsconfig.json to avoid missing type errors [OK]
Common Mistakes:
  • Removing lang="ts" instead of fixing config
  • Thinking Vue disallows TypeScript in .vue files
  • Renaming .vue files to .ts incorrectly
5. You want to use TypeScript in a Vue 3 project with the Composition API. Which setup is correct for the script block?
hard
A. <script lang="js" setup> import { ref } from 'vue' const count = ref(0) </script>
B. <script> import { ref } from 'vue' const count = ref(0) </script>
C. <script lang="ts" setup> import { ref } from 'vue' const count = ref(0) </script>
D. <script lang="ts"> export default { data() { return { count: 0 } } } </script>

Solution

  1. Step 1: Identify TypeScript and Composition API usage

    Using lang="ts" setup enables TypeScript and Composition API syntax in Vue 3.
  2. Step 2: Check other options

    The version with lang="js" setup uses JavaScript not TypeScript, the plain <script> lacks the lang="ts" attribute, and the one using export default with data() is Options API, not Composition API.
  3. Final Answer:

    <script lang="ts" setup> with ref import and usage -> Option C
  4. Quick Check:

    Use lang="ts" setup for Composition API with TypeScript [OK]
Hint: Use
Common Mistakes:
  • Omitting lang="ts" for TypeScript
  • Using lang="js" instead of lang="ts"
  • Mixing Options API with Composition API syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard