【发布时间】:2020-06-16 05:52:47
【问题描述】:
我正在尝试找出泛型并做一个特殊情况,即我根据另一种类型存储一个类型,然后在运行时创建它。
- 我已经看到了,我的界面因我的属性而失败: typeof abstract class and extendors
- 我也试图理解: https://www.typescriptlang.org/docs/handbook/utility-types.html
我的方法很糟糕,但我知道必须有更好的方法。
我想要什么:
abstract class AFruit{
isBad:boolean;
}
class Apple extends AFruit {}
class Orange extends AFruit {}
//problem
interface Storage
{
fruit: type-base-of-abstract-fruit; //this is what I'd like to figure out
}
//my version
interface Storage
{
fruit: typeof Apple | typeof Orange;
}
//finally, my example usage:
{fruit: Apple}
//or
{fruit: Orange}
我确实想稍后更新它以使用 Apple 或 Orange,但我只是还没有弄清楚 typescript 泛型方式。
【问题讨论】:
标签: typescript