扩展泛型类型类的打字稿

时间:2021-01-19 03:04:38

标签: typescript

这是我要扩展的类

import 'reflect-metadata';
import { IRepository, IFireOrmQueryLine, IOrderByParams, IEntity } from './types';
import { AbstractFirestoreRepository } from './AbstractFirestoreRepository';
import { TransactionRepository } from './Transaction/BaseFirestoreTransactionRepository';
export declare class BaseFirestoreRepository<T extends IEntity> extends AbstractFirestoreRepository<T> implements IRepository<T> {
    private readonly firestoreColRef;
    constructor(colName: string, collectionPath?: string);
    findById(id: string): Promise<T>;
    create(item: T): Promise<T>;
    update(item: T): Promise<T>;
    delete(id: string): Promise<void>;
    runTransaction<R>(executor: (tran: TransactionRepository<T>) => Promise<R>): Promise<R>;
    createBatch(): import("./Batch/FirestoreBatchSingleRepository").FirestoreBatchSingleRepository<T>;
    execute(queries: Array<IFireOrmQueryLine>, limitVal?: number, orderByObj?: IOrderByParams, single?: boolean): Promise<T[]>;
}

如您所见,它具有扩展到 IEntity 的 T

目前,这是我的代码

import { BaseFirestoreRepository, IEntity } from 'fireorm'

interface ICustomRepository<T extends IEntity> {
  where(filter: any): BaseFirestoreRepository<T>
}

BaseFirestoreRepository.prototype.where = function () {}

我不确定它是否正确,因为我在执行 prototype 时没有传递类型,而且 where 显示错误 Property 'where' does not exist on type 'BaseFirestoreRepository<any>'

我在这里的最终目标是使用类似于这个 c# 扩展的这个函数

public static IQueryable<T> OrderByPropertyName<T>(this IQueryable<T> query, string attribute, string direction) {
    // my logic here
}

我可以直接在类中添加类似于原型工作方式的方法

1 个答案:

答案 0 :(得分:0)

interface ICustomRepository<T extends IEntity> 
  extends BaseFirestoreRepository<T> {
  ...
}