Building a Todo App with Zustand and TypeScript

Rind Devran Tukan
•< 1 min read
Reading Time: < 1 minute
TypeScript Setup
interface Todo {
id: string;
text: string;
done: boolean;
}
interface TodoStore {
todos: Todo[];
addTodo: (text: string) => void;
toggleTodo: (id: string) => void;
}
const useStore = create<TodoStore>((set) => ({
todos: [],
addTodo: (text) => set((state) => ({
todos: [...state.todos, { id: Date.now().toString(), text, done: false }],
})),
toggleTodo: (id) => set((state) => ({
todos: state.todos.map((todo) =>
todo.id === id ? { ...todo, done: !todo.done } : todo
),
})),
}));
UI Implementation
function TodoList() {
const { todos, addTodo, toggleTodo } = useStore();
const [input, setInput] = useState('');
return (
<div>
<input value={input} onChange={(e) => setInput(e.target.value)} />
<button onClick={() => addTodo(input)}>Add</button>
{todos.map((todo) => (
<div key={todo.id} onClick={() => toggleTodo(todo.id)}>
{todo.text} {todo.done ? '✓' : ''}
</div>
))}
</div>
);
}
Conclusion
A fully type-safe Todo app with Zustand and TypeScript.