jeudi 27 octobre 2016

TypeScirpt: Class generics - something like Java's Class

I have a TypeScript code that processes classes (OGM framework).

There is a base class for models/entities, say, BaseModel. However, I would like to support several different base models, e.g. ProxiedBaseModel and DecoratedBaseModel.

The issue is:

export abstract class DiscriminatorMapping<BaseModelT>
{
    public static getMapping() : 
        { [key: string]: typeof BaseModelT } 
    { ... }

This is a simple dictionary-like class.
It needs a map of string <-> class.
And typeof BaseModelT obviously doesn't work as TypeScript can't know what type will come in.

How should I express that the class type parameter takes something that is a class?
I already know how to represent the type of any class, that's { new (): <any> }. But I don't know how to apply it here:

class DiscriminatorMapping<BaseModelT extends { new (): <any> }>

Maybe I should introduce some type alias?





Aucun commentaire:

Enregistrer un commentaire