为什么我的 generic of generic 不能按预期工作?
Why my generic of generic doesn't work as expected?
编辑:这里有一个最小的例子here
我将 TypeScript 与 TypeORM 库一起使用。这是 "base" Repository
通用定义:
class Repository<Entity extends ObjectLiteral> {
find(conditions?: FindConditions<Entity>): Promise<Entity[]>;
}
如果我尝试扩展此 class,将 Bank
class 作为 Entity
传递,它按预期工作(我在 find
方法中得到自动完成:
class Bank {
name: string;
}
class BankRepository extends Repository<Bank> {
public test():void {
this.find({ name: 'foo' }); // OK!
}
}
但是,如果我尝试使 my 通用,使用 BankModel
抽象 class:
abstract class BankModel {
foo: string;
}
class BankRepository<E extends BankModel> extends Repository<E> {
test(foo: string): void {
this.find({ foo: foo }); // KO!!!
}
}
错误是:
Argument of type '{ foo: string; }' is not assignable to parameter of type 'FindConditions'.ts(2345),
FindConditions<E>
的声明是:
declare type FindConditions<T> = {
[P in keyof T]?: FindConditions<T[P]> | FindOperator<FindConditions<T[P]>>;
};
所以..为什么它不起作用?
我发现什么是 Typescript 错误。我使用 3.6.3 版。
编辑:这里有一个最小的例子here
我将 TypeScript 与 TypeORM 库一起使用。这是 "base" Repository
通用定义:
class Repository<Entity extends ObjectLiteral> {
find(conditions?: FindConditions<Entity>): Promise<Entity[]>;
}
如果我尝试扩展此 class,将 Bank
class 作为 Entity
传递,它按预期工作(我在 find
方法中得到自动完成:
class Bank {
name: string;
}
class BankRepository extends Repository<Bank> {
public test():void {
this.find({ name: 'foo' }); // OK!
}
}
但是,如果我尝试使 my 通用,使用 BankModel
抽象 class:
abstract class BankModel {
foo: string;
}
class BankRepository<E extends BankModel> extends Repository<E> {
test(foo: string): void {
this.find({ foo: foo }); // KO!!!
}
}
错误是:
Argument of type '{ foo: string; }' is not assignable to parameter of type 'FindConditions'.ts(2345),
FindConditions<E>
的声明是:
declare type FindConditions<T> = {
[P in keyof T]?: FindConditions<T[P]> | FindOperator<FindConditions<T[P]>>;
};
所以..为什么它不起作用?
我发现什么是 Typescript 错误。我使用 3.6.3 版。