Complete the code to import Pinia's store creation function.
import { [1] } from 'pinia';
The defineStore function is used to create a Pinia store.
Complete the code to define a Pinia store named 'counter'.
export const useCounterStore = [1]('counter', { state: () => ({ count: 0 }) });
defineStore is used to create a named store in Pinia.
Fix the error in the state typing to make the store type-safe.
export const useUserStore = defineStore('user', { state: (): [1] => ({ name: '', age: 0 }) });
Using a specific interface like UserState ensures type safety for the state.
Fill both blanks to add a typed getter and an action in the Pinia store.
export const useTodoStore = defineStore('todo', { state: () => ({ todos: [] as string[] }), getters: { todoCount: (state) => state.[1].length }, actions: { addTodo(todo: string) { this.[2].push(todo); } } });
The state property is named todos, so both getter and action must use it.
Fill all three blanks to create a fully typed Pinia store with state, getter, and action.
interface ProductState {
products: string[];
}
export const useProductStore = defineStore('product', {
state: (): [1] => ({ products: [] }),
getters: {
productCount: (state) => state.[2].length
},
actions: {
addProduct(product: string) {
this.[3].push(product);
}
}
});The state is typed as ProductState, and the property is products used in getter and action.
