英文:
Typescript instance of a class in variable
问题
我正在尝试在TypeScript中创建一个函数,该函数以一个类作为参数(不是实例,而是类本身),并返回该类的一个实例,但找不到有效的方法来实现这一点。
以下是代码示例:
public getService<T extends new (appInstances: AppInstances) => Y, Y extends AbstractService>(serviceClass: T) {
const service = this.services.find(
(service) => (service as unknown) instanceof serviceClass
);
if (!service) {
throw new Error(
'instance of service ' + serviceClass.name + ' not found'
);
}
return service as unknown as Y;
}
// 此处的类型是任意的,应该是CommandInteractionService
const commandInteractionService = this.getAppInstances().serviceManager.getService(CommandInteractionService);
是否有适当的方法来实现这一目标,或者是否有一种解决方法?
英文:
I'm trying to create in TypeScript a function that takes a class as an argument (not an instance but the class itself) and returns an instance of that class, but can't find an effective way to do it.
Code example below:
public getService<T extends new (appInstances: AppInstances) => Y, Y extends AbstractService>(serviceClass: T) {
const service = this.services.find(
(service) => (service as unknown) instanceof serviceClass
);
if (!service) {
throw new Error(
'instance of service ' + serviceClass.name + ' not found'
);
}
return (service as unknown) as Y;
}
// type here is any and should be CommandInteractionService
const commandInteractionService = this.getAppInstances().serviceManager.getService(CommandInteractionService);
Is there a proper way to achieve this, or is there a workaround?
答案1
得分: 0
public getService<T extends new (...args: any[]) => Y, Y>(serviceClass: T): Y {
const service = this.services.find((service) => service instanceof serviceClass);
if (!service) {
throw new Error('找不到服务实例:' + serviceClass.name);
}
return service as Y;
}
英文:
something like this?
public getService<T extends new (...args: any[]) => Y, Y>(serviceClass: T): Y {
const service = this.services.find((service) => service instanceof serviceClass);
if (!service) {
throw new Error('Instance of service ' + serviceClass.name + ' not found');
}
return service as Y;
}
答案2
得分: 0
我刚刚找到了解决方案,我在这里发布它,因为找到它并不容易。希望将来绝望的人能找到这个有用。
public getService<T extends new (...args: ConstructorParameters<typeof AbstractService>) => AbstractService>(serviceClass: T): InstanceType<T> {
const service = this.services.find(
(service) => service instanceof serviceClass,
);
if (!service || serviceClass === AbstractService) {
throw new Error(
'未找到服务的实例:' + serviceClass.name,
);
}
return service as InstanceType<T>;
}
您可以在官方 TypeScript 文档 https://www.typescriptlang.org/docs/handbook/utility-types.html 上找到关于 InstanceType 和 ConstructorParameters 的文档。
英文:
edit
I just found the solution, I am posting it here since it wasn't easy to find. Hoping that future desperate souls find this useful.
public getService<T extends new (...args: ConstructorParameters<typeof AbstractService>) => AbstractService>(serviceClass: T): InstanceType<T> {
const service = this.services.find(
(service) => service instanceof serviceClass,
);
if (!service || serviceClass === AbstractService) {
throw new Error(
'instance of service ' + serviceClass.name + ' not found',
);
}
return service as InstanceType<T>;
}
You can find documentation about InstanceType and ConstructorParameters on the official TypeScript documentation https://www.typescriptlang.org/docs/handbook/utility-types.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论