Is there a way to alias a longwinded generic type in the scope of a class?

无人久伴 提交于 2020-01-06 08:18:34

问题


I have a shared repository class using a shared entity/document type, that looks kind of like this:

type IEntity<TData extends {}, TType extends string = string> = TData & {
    id: string;
    type: TType;
    createdAtIso: string;
}

class Repository<TData extends {}, TType extends string = string> {
    constructor(private _type: TType) { }

    save(data: TData): IEntity<TData, TType> {
        return this._save({
            id: 'asdf',
            type: this._type,
            createdAtIso: new Date().toISOString(),
            ...data
        });
    }

    _save(entity: IEntity<TData, TType>): IEntity<TData, TType> {
        // save entity here
        return entity;
    }
}

The point is to encapsulate the book-keeping in TEntity, and have the code using the repo only have to care about what it specifically wants to store.

That said, this means the construct IEntity<TData, TType> is repeated a zillion times within the repository class. At module toplevel, I could use a type alias to create a shorter name for the type; is there any construct I could use to alias a type within a class declaration, using the generic type arguments it takes?

来源:https://stackoverflow.com/questions/55233473/is-there-a-way-to-alias-a-longwinded-generic-type-in-the-scope-of-a-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!