[英]Typescript TypeORM find return type mismatch with defiend entity type
I have the following code snippet in infrastructure
module:我在
infrastructure
模块中有以下代码片段:
import { Student } from "core"
import { Repository } from "./Repository"
import { Database } from "../../db"
export class UserRepository<Student> extends Repository<Student> {
private _repository = Database.AppDataSource.getRepository(Student);
public override async GetById (id: number): Promise<Student | null> {
return await this._repository.findOneBy({ id: id });
}
public override async ListAll (): Promise<Student[]> {
return await this._repository.find();
}
}
The type Student
is defined in core
module. Student
类型在core
模块中定义。 Build error:构建错误:
Data/Repositories/StudentRepository.ts:7:9 - error TS2322: Type 'import("/usr/src/Node.JSRestAPI/src/core/build/index").Student | null' is not assignable to type 'Student | null'.
Type 'import("/usr/src/Node.JSRestAPI/src/core/build/index").Student' is not assignable to type 'Student'.
'Student' could be instantiated with an arbitrary type which could be unrelated to 'import("/usr/src/Node.JSRestAPI/src/core/build/index").Student'.
7 return await this._repository.findOneBy({ id: id });
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Data/Repositories/StudentRepository.ts:10:9 - error TS2322: Type 'import("/usr/src/Node.JSRestAPI/src/core/build/index").Student[]' is not assignable to type 'Student[]'.
Type 'import("/usr/src/Node.JSRestAPI/src/core/build/index").Student' is not assignable to type 'Student'.
'Student' could be instantiated with an arbitrary type which could be unrelated to 'import("/usr/src/Node.JSRestAPI/src/core/build/index").Student'.
10 return await this._repository.find();
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
What do I miss?我想念什么?
If you want to return the Promise
s you should remove the async await
:如果你想返回
Promise
你应该删除async await
:
export class UserRepository<Student> extends Repository<Student> {
private _repository = Database.AppDataSource.getRepository(Student);
public override GetById (id: number): Promise<Student | null> {
return this._repository.findOneBy({ id: id });
}
public override ListAll (): Promise<Student[]> {
return this._repository.find();
}
}
Why are you importing Student from the core library, instead of some entity file, where you defined your class?为什么要从定义 class 的核心库而不是某些实体文件中导入 Student?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.