deepkit-framework
deepkit-framework copied to clipboard
deserialize generic type
Im using angular 15 and @deepkit/type for deserializing json objects. when i use it in a simple class and specify a certain type it works perfect. But when i use it in the abstract class, like this:
export abstract class AbstractTableService<T extends TableData> {
......
loadData(params: ListQueryParams): Observable<T[]> {
return this.client.post(
this.apiPath + ACTION.list,
params
).pipe(
map(json => deserialize<T[]>(json.data.list)),
catchError(() => EMPTY),
take(1)
);
}
.......
}
and then inherit this class:
export class UserTableService extends AbstractTableService<User> implements OnDestroy {........}
Im getting array of nulls. What i doing wrong or this is not possible to do so?
That's not possible since T is not inferable in runtime. You can pass around actual class references, then you could pass that instead of the type deserialize(json.data.list, undefined, undefined, undefined, typeOf(this.classType)).
@marcj is there any other way to infer T? Is it ts/reflection limitation?