Given this selector that derives a list of completed tasks from the state, what will be the output when the state has two tasks, one completed and one not?
import { createSelector } from '@ngrx/store'; interface Task { id: number; title: string; completed: boolean; } interface AppState { tasks: Task[]; } const selectTasks = (state: AppState) => state.tasks; const selectCompletedTasks = createSelector( selectTasks, (tasks) => tasks.filter(task => task.completed) ); const state: AppState = { tasks: [ { id: 1, title: 'Learn Angular', completed: true }, { id: 2, title: 'Write tests', completed: false } ] }; const result = selectCompletedTasks(state);
Think about what the filter condition task.completed does.
The selector filters tasks to only those where completed is true. Only the first task meets this condition.
Choose the correct Angular selector code that returns the number of completed tasks from the state.
interface Task { id: number; completed: boolean; }
interface AppState { tasks: Task[]; }
const selectTasks = (state: AppState) => state.tasks;Count means a number, not an array.
Option A filters completed tasks and returns the length, which is the count. Option A tries to sum booleans but will not work correctly because booleans are not numbers in this context. Option A returns the length of all tasks, not just completed ones. Option A returns an array, not a count.
Consider this selector code. It throws an error when used. What is the cause?
const selectTasks = (state: any) => state.tasks; const selectFirstCompletedTask = createSelector( selectTasks, (tasks) => tasks.find(task => task.completed).title );
What happens if find does not find any matching item?
If no task is completed, find returns undefined. Trying to access .title on undefined causes a runtime error.
Why do Angular developers use selectors to compute derived state instead of computing it directly in components?
Think about how selectors help with performance and reusability.
Selectors cache their output and only recompute when inputs change, which helps components avoid expensive recalculations and improves app performance.
Given the following selectors and state, what will selectUserCompletedTaskTitles return?
interface Task { id: number; title: string; completed: boolean; userId: number; }
interface User { id: number; name: string; }
interface AppState { tasks: Task[]; users: User[]; }
const selectTasks = (state: AppState) => state.tasks;
const selectUsers = (state: AppState) => state.users;
const selectCurrentUserId = (state: AppState) => 2;
const selectUserTasks = createSelector(
selectTasks,
selectCurrentUserId,
(tasks, userId) => tasks.filter(task => task.userId === userId)
);
const selectUserCompletedTaskTitles = createSelector(
selectUserTasks,
(tasks) => tasks.filter(t => t.completed).map(t => t.title)
);
const state: AppState = {
tasks: [
{ id: 1, title: 'Task A', completed: true, userId: 1 },
{ id: 2, title: 'Task B', completed: false, userId: 2 },
{ id: 3, title: 'Task C', completed: true, userId: 2 }
],
users: [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' }
]
};
const result = selectUserCompletedTaskTitles(state);Filter tasks by current user ID, then by completed status, then get titles.
The current user ID is 2. Tasks for user 2 are Task B (not completed) and Task C (completed). So only 'Task C' is returned.