Typescript在泛型类型声明中看不到导入的变量

时间:2019-06-21 06:48:54

标签: javascript node.js mongoose

我正在尝试为应用程序中的每个实体创建一个通用存储库

mongo-repository.ts

import { Document, Model, Types } from 'mongoose';

type MongooseModel<T> = Model<T & Document>;

export abstract class MongoRepository<T extends MongooseModel<T>> {
  protected constructor(
    protected readonly model: T,
  ) {}
}

user.repository.ts

import { User } from '../../../models/User';
import { MongoRepository } from '../../common/mongo/mongo-repository';

class Repository extends MongoRepository<User> {
  constructor() {
    super(User);
  }
}

export const UserRepository = new Repository();

实际结果:

src/modules/user/repository/user.repository.ts:4:42 - error TS2304: Cannot find name 'User'.

4 class Repository extends MongoRepository<User> {
                                           ~~~~

预期结果: 工作。

但是,我没有在super(User)中得到此错误消息,而只是在通用声明中得到了

1 个答案:

答案 0 :(得分:0)

已修复, 修复:

MongoRepository<T extends typeof Model>
class Repository extends MongoRepository<typeof User>