LabEG / Serializable

Small library for deserialization and serialization for javascript and typescript
MIT License
62 stars 10 forks source link

Generic types #1

Closed nexen505 closed 5 years ago

nexen505 commented 5 years ago

How to handle generic classes?

For example, I have class

export class Tuple2<T1 extends Serializable, T2 extends Serializable> extends Serializable {
    @jsonProperty(T1)
    private item1: T1;

    @jsonProperty(T2)
    private item2: T2;
}

How to handle this?

LabEG commented 5 years ago

Generic exists only at the stage of writing code. There is no information about him in runtime.

I use the following code to solve the problem.

export UserPagedList extends PagedList<User>{

    @jsonProperty([Use])
    public elements: User[] = [];

}

export abstract class PagedList<TEntity> extends Serializable {

    public abstract elements: TEntity[] = [];

    @jsonProperty(Number)
    public pageNumber: number = 0;

    ...

    @jsonProperty(Number)
    public totalElements: number = 0;

}

But if you offer a more beautiful version, I will implement it.