typescript - 在 Typescript 中将元组类型转换为类型数组
问题描述
我正在尝试在香草打字稿中复制 Angular 的依赖注入行为。我的方法是将构造函数的参数类型作为元组类型获取,将其转换为类型数组,然后查看字典对象或数组并从该结构中获取其类型与数组位置中的类型匹配的对象。
在下面的代码中,我定义了一个测试类,其中构造函数采用苹果和橙子。在类定义下方,我可以将每个参数的类型转换为一个名为“TestClassParameters”的元组类型。
有什么方法可以将这种类型: [apple,orange] 转换为数组对象 => [typeof apple,typeof orrange]?
class Apple {}
class Orange { }
class TestClass {
private apple : Apple;
private orange: Orange;
constructor(private apple: Apple, private orange: Orange) {}
}
type TestClassParameters = ConstructorParameters<typeof TestClass>;
解决方案
推荐阅读
- c - 使用 cmake build xbindkeys:error unknown type name 'Display'
- typescript - Typescript:如何为增量编译和 strictNull 检查计算文件依赖关系
- python - 如何根据索引转置值?
- c++ - 在 C++ char 数组中打印元音
- android - Firebase 功能在模拟器中运行良好,但在实际设备中使用时会引发“超出期限”错误。有人面临同样的问题吗?
- flutter - 扑动时无法识别 toLowerCase() 的 Firestore 在 null 上调用了方法 toUpperCase()
- html - 如何编辑此代码以将我的代码的一部分设置为在移动设备上的此网站上有所不同?
- python - Python numpy 操作无法正确计算
- keras - Keras图像二进制分类,哪个类被分配概率0和1?使用函数式 API
- java - Spring:在事件发布者而不是侦听器上设置 TransactionPhase