I have a DataHandler service that calls a database service and returns the value to the calling component. The database service returns an observable of an array, while I'd like the data handler to return a signal of the same array ready to use for the component. Thus, I'm trying to use angular's toSignal function but when I set an empty array as initial value I receive the following error:
Type 'never[]' is not assignable to type 'undefined'.
As I'm understanding, TypeScript can't find the type of the empty array, hence it tries to set the type of the array to the mentioned never[]
. However, I am defining the type twice, in the function call of the Data Handler, the return type is defined as:
public readOverview(type: DataType): Observable<DataEntry[]> { ... }
So the returned Observable has the type DataEntry[]. The type is also specifically set in the toSignal function call I'm making in the data handler:
return toSignal<DataEntry[]>(this._dbFacade.readOverview(type), { initialValue: [] });
The way I see it, TypeScript should definitely be able to find the type DataEntry[] for the empty inital array. Yet, it doesn't. So what do I have to do to make it find the proper type and be able to use an empty array as initial value?
<DataEntry[]>
from toSignal call. this way correct generic type will be inferred. other way is to pass it 2 timestoSignal<DataEntry[], DataEntry[]>