0
0
Vueframework~20 mins

Type-safe stores with Pinia in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pinia Type Safety Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Pinia store state access?
Given the following Pinia store, what will be logged when console.log(store.user.name) runs?
Vue
import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => ({
    user: {
      name: 'Alice',
      age: 30
    }
  })
})

const store = useUserStore()
console.log(store.user.name)
ATypeError: Cannot read property 'name' of undefined
B"user"
Cundefined
D"Alice"
Attempts:
2 left
💡 Hint
Look at how the state is defined and accessed through the store instance.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a type-safe Pinia store state?
Choose the option that correctly defines a Pinia store with TypeScript types for state.
Vue
import { defineStore } from 'pinia'

interface Todo {
  id: number
  title: string
  done: boolean
}

export const useTodoStore = defineStore('todo', {
  state: () => ({
    todos: [] as Todo[]
  })
})
Astate: () => ({ todos: [] as Todo[] })
Bstate: () => ({ todos: [] }) as Todo[]
Cstate: () => <Todo[]>[]
Dstate: (): Todo[] => []
Attempts:
2 left
💡 Hint
Check how the array is typed inside the state function.
🔧 Debug
advanced
2:00remaining
Why does this Pinia store cause a runtime error?
Identify the cause of the runtime error in this Pinia store code.
Vue
import { defineStore } from 'pinia'

export const useCounterStore = defineStore('counter', {
  state: () => ({
    count: 0
  }),
  actions: {
    increment() {
      this.count++
    }
  }
})

const store = useCounterStore()
store.increment()
console.log(store.count)
ASyntaxError: Unexpected token in actions
BTypeError: Cannot read property 'count' of undefined
CNo error; output is 1
DReferenceError: useCounterStore is not defined
Attempts:
2 left
💡 Hint
Check how actions access state properties using 'this'.
state_output
advanced
2:00remaining
What is the value of store.fullName after calling store.setName('Bob', 'Smith')?
Consider this Pinia store with getters and actions. What will store.fullName be after calling store.setName('Bob', 'Smith')?
Vue
import { defineStore } from 'pinia'

export const useProfileStore = defineStore('profile', {
  state: () => ({
    firstName: '',
    lastName: ''
  }),
  getters: {
    fullName: (state) => `${state.firstName} ${state.lastName}`
  },
  actions: {
    setName(first, last) {
      this.firstName = first
      this.lastName = last
    }
  }
})

const store = useProfileStore()
store.setName('Bob', 'Smith')
A"Smith Bob"
B"Bob Smith"
C"Bob"
D""
Attempts:
2 left
💡 Hint
Look at how the getter combines state properties.
🧠 Conceptual
expert
3:00remaining
Which option best explains how Pinia ensures type safety in stores?
Select the explanation that correctly describes how Pinia achieves type safety when using TypeScript.
APinia uses TypeScript generics and infers types from the state, getters, and actions to provide full type safety and autocompletion.
BPinia requires manual type annotations on every property to enforce type safety, otherwise it defaults to any type.
CPinia disables type checking inside stores to improve performance and relies on runtime checks instead.
DPinia only supports type safety for state but not for getters or actions.
Attempts:
2 left
💡 Hint
Think about how TypeScript works with Pinia's store definitions.