📖 Scenario: You are building a simple Vue app to manage a list of tasks. You want to keep the tasks in a central store using Pinia. To avoid mistakes, you will make the store type-safe using TypeScript.
🎯 Goal: Create a Pinia store with TypeScript that holds a list of tasks. Each task has an id (number) and a title (string). Add a getter to count tasks and an action to add a new task.
📋 What You'll Learn
Create a Pinia store named useTaskStore using defineStore
Define a TypeScript interface Task with id: number and title: string
Store state as an array of Task objects named tasks
Add a getter taskCount returning the number of tasks
Add an action addTask that takes a Task and adds it to tasks
💡 Why This Matters
🌍 Real World
Type-safe stores help prevent bugs by ensuring data shapes are consistent. This is useful in any Vue app managing shared state.
💼 Career
Many Vue jobs require managing state with Pinia and TypeScript. Knowing how to create type-safe stores is a valuable skill.
Progress0 / 4 steps
1
Set up the Task interface and initial state
Create a TypeScript interface called Task with id as a number and title as a string. Then create a Pinia store named useTaskStore using defineStore with an empty array of tasks as the state.
Vue
Hint
Use interface Task { id: number; title: string } and state: () => ({ tasks: [] as Task[] }).
2
Add a getter to count tasks
Add a getter named taskCount inside useTaskStore that returns the length of the tasks array.
Vue
Hint
Use getters: { taskCount: (state) => state.tasks.length }.
3
Add an action to add a new task
Add an action named addTask inside useTaskStore that takes a parameter task of type Task and pushes it into the tasks array.
Vue
Hint
Use actions: { addTask(task: Task) { this.tasks.push(task) } }.
4
Export the store and use it in a Vue component
Ensure the store useTaskStore is exported. Then, in a Vue component, import useTaskStore and create a store instance by calling it inside setup().
Vue
Hint
Import useTaskStore and call it inside setup() to create taskStore.
Practice
(1/5)
1. What is the main benefit of using type-safe stores with Pinia in a Vue application?
easy
A. It speeds up the app by caching data on the server.
B. It automatically styles components based on state.
C. It replaces Vue Router for navigation.
D. It helps catch errors by checking data types during development.
Solution
Step 1: Understand type safety in programming
Type safety means the system checks that data matches expected types, reducing bugs.
Step 2: Connect type safety to Pinia stores
Pinia stores with types ensure state and actions use correct data types, catching mistakes early.
Final Answer:
It helps catch errors by checking data types during development. -> Option D
Quick Check:
Type safety = catches type errors [OK]
Hint: Type-safe means checking data types to avoid bugs [OK]
Common Mistakes:
Confusing type safety with styling or UI features
Thinking Pinia replaces routing
Assuming type safety improves runtime speed
2. Which of the following is the correct way to define a Pinia store with TypeScript for a state property count of type number?
A. Using string 'zero' to assign to number state property count causes a type error.
B. The increment method should use this.count++ instead of addition.
C. State must be an object, not a function returning an object.
D. The store name 'main' is invalid and must be uppercase.
Solution
Step 1: Check state type and assignments
The state count is a number initialized to 0.
Step 2: Identify type mismatch in actions
In reset(), assigning string 'zero' to count breaks type safety, causing error.
Final Answer:
Using string 'zero' to assign to number state property count causes a type error. -> Option A
Quick Check:
Assigning wrong type to state = Using string 'zero' to assign to number state property count causes a type error. [OK]
Hint: State types must match assigned values exactly [OK]
Common Mistakes:
Ignoring type mismatch errors
Thinking addition syntax is wrong
Believing state must be plain object, not function
Assuming store names must be uppercase
5. You want to create a type-safe Pinia store for a user profile with state properties name (string), age (number), and an action updateProfile that accepts a partial object to update state. Which approach correctly ensures type safety?
hard
A. Define an interface for state, use it in store, and type updateProfile parameter as Partial<State>.
B. Use any type for state and action parameters to allow flexibility.
C. Define state without types and cast inside updateProfile to avoid errors.
D. Use separate stores for each property to avoid typing complexity.
Solution
Step 1: Define a TypeScript interface for the state
Creating an interface with name: string and age: number sets clear expected types.
Step 2: Use Partial<State> in updateProfile parameter
This allows updating any subset of properties while keeping type safety.
Step 3: Implement store using interface and typed action
Using the interface in defineStore and typing updateProfile ensures safe updates.
Final Answer:
Define an interface for state, use it in store, and type updateProfile parameter as Partial<State>. -> Option A
Quick Check:
Interface + Partial parameter = Define an interface for state, use it in store, and type updateProfile parameter as Partial<State>. [OK]
Hint: Use interfaces and Partial type for safe partial updates [OK]
Common Mistakes:
Using any type loses type safety
Casting types inside actions hides errors
Splitting state unnecessarily complicates design
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.