console.log(store.user.name) runs?import { defineStore } from 'pinia' export const useUserStore = defineStore('user', { state: () => ({ user: { name: 'Alice', age: 30 } }) }) const store = useUserStore() console.log(store.user.name)
The store's state has a user object with a name property set to "Alice". Accessing store.user.name returns "Alice".
import { defineStore } from 'pinia' interface Todo { id: number title: string done: boolean } export const useTodoStore = defineStore('todo', { state: () => ({ todos: [] as Todo[] }) })
Option A correctly uses a type assertion inside the returned object to type todos as Todo[]. Other options either misplace the type or cause syntax errors.
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)
The code correctly defines an action that increments count using this.count++. Calling increment() updates the state, so logging store.count outputs 1.
store.fullName after calling store.setName('Bob', 'Smith')?store.fullName be after calling store.setName('Bob', 'Smith')?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')
The getter fullName returns the concatenation of firstName and lastName separated by a space. After calling setName('Bob', 'Smith'), the state updates accordingly, so fullName is "Bob Smith".
Pinia leverages TypeScript's powerful type inference and generics to automatically infer types from the store's state, getters, and actions. This provides developers with type safety and autocompletion without needing excessive manual annotations.