There are a number of these questions on SO, but I still can't seem to find one that helps me figure out what's going on here.
Context, I'm using Pinia (VueJS), and I'm creating a "global" store, for a whole bunch of common actions/state that exist across many modules.
import find from 'lodash/find';
export interface IGenericPaginationStore<T> {
list: T[];
count: number;
page: number;
next: string | null;
previous: string | null;
active: T | null;
pageSize: number;
}
// store.ts
setActive<T extends { uuid: string }>(state: IGenericPaginationStore<T>, args: { uuid?: string | null, resource?: T | null }) {
if (args.resource) {
state.active = args.resource
state.list.forEach((resource: T, index) => {
if (resource?.uuid === args.resource?.uuid) {
state.list[index] = args.resource // **** ERROR 1 HERE ****
}
})
return;
}
},
Error 1:
TS2322: Type 'T | null | undefined' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'T | null | undefined'.
I just can't figure out why this doesn't work. Any help would be greatly appreciated.