我想傳遞類的類型信息,同時暗示編譯器指出給定的參數不是類的實例,而是類定義。我怎樣才能做到這一點?如何在類型描述中指定類定義作爲參數類型
import * as Routes from '../routes';
import * as Entities from '../entities';
export class RouteManager {
public router = Router();
private connection = getConnectionManager().get();
constructor() {
this.addRoute(Routes.VideoRoute, Entities.Video);
}
addRoute(routeClass: AbstractRoute<AbstractEntity>, entity: AbstractEntity): void {
const instance = new routeClass(entity, this.connection.getRepository(entity))
this.router.get(instance.route, instance.find);
}
}
在這裏,編譯器會抱怨的new routeClass(entity, this.connection.getRepository(entity))
線,因爲它認爲routeClass
是AbstractRoute<AbstractEntity>
一個實例,而不是出口類定義它。
我嘗試過使用AbstractRoute<AbstractEntity>.constructor
,但Typescript似乎並不知道這個結構。
您鏈接到這個問題o.O –
對不起! :D http://stackoverflow.com/questions/34698710/defining-typescript-generic-type-with-new –