deepkit-framework icon indicating copy to clipboard operation
deepkit-framework copied to clipboard

deserialize generic type

Open maxtorchel opened this issue 3 years ago • 2 comments

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?

maxtorchel avatar Jan 16 '23 14:01 maxtorchel

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 avatar Feb 07 '23 23:02 marcj

@marcj is there any other way to infer T? Is it ts/reflection limitation?

marcinvalas avatar Jul 27 '23 09:07 marcinvalas