Interface:
export interface User {
userId: number;
userName: string;
emailId: string;
role: string;
}
export interface UserState {
users: User[];
_users: User[];
}
export const initialUserState: UserState = {
users: [],
_users: []
};
Signal store:
import { httpResource } from '@angular/common/http';
import { signalStore, withComputed, withProps, withState } from '@ngrx/signals';
import { initialUserState, User } from './user.slice';
const basePath = 'https://api.freeprojectapi.com/api/BusBooking';
const getAllUsers = `${basePath}/GetAllUsers`;
export const USERSTORE = signalStore(
withState(initialUserState),
withProps(() => {
const users = httpResource<User[]>(() => getAllUsers);
return { _users: users };
}),
withComputed((store: { users: User[]; _users: any }) => ({
users: () => store._users.value() ?? []
}))
);
I am getting withComputed errors as:
Argument of type '(store: { users: User[]; _users: any; }) => { users: () => any; }' is not assignable to parameter of type '(store: { users: Signal<User[]>; _users: (() => User[]) & { [SIGNAL]: unknown; } & HttpResourceRef<User[] | undefined>; }) => { users: () => any; }'.
Types of parameters 'store' and 'store' are incompatible.
Type '{ users: Signal<User[]>; _users: (() => User[]) & { [SIGNAL]: unknown; } & HttpResourceRef<User[] | undefined>; }' is not assignable to type '{ users: User[]; _users: any; }'.
Types of property 'users' are incompatible.
Type 'Signal<User[]>' is not assignable to type 'User[]'.ts(2345)