【发布时间】:2017-10-24 19:20:25
【问题描述】:
是否可以将字符串传递给函数并分配返回的动态类型?
class MyService {
aMethod(arg) {
console.log(arg)
}
}
Container.register('my_service', MyService);
// Internally Container holds a Object
export interface IServiceInstance {
name: string;
type: Function;
instance: Object;
}
const myServiceInstance = Container.get('my_service')
// myServiceInstance should be detected as type of MyService class
myServiceInstance.aMethod('Hello World')
// Currently I use it like this. But if it is possible I want to get rid of that
const myServiceInstance = (Container.get('my_service') as MyService)
【问题讨论】:
-
什么是
Container,它支持泛型吗?Container.get<MyService>('my_service'),例如。您必须具体说明 somewhere 的类型。 -
不是
Container.get<MyService>('my_service')与(Container.get('my_service') as MyService)完全相同 -
效果一样。 TS 不是魔术。同样,您必须指定某处。
标签: typescript