我正在试验一些相当先进的 TS 来支持我们加载和解析 xml 负载的方式。
此有效负载可以包含多个模块(我们可以在 URL 中指定哪个模块),对于选择的每个模块,我想运行一个解析函数并将结果合并到一个对象中。
这是我的 TS playground。我已经尽可能地简化了我的代码,但没有完全抽象。
问题发生在第 40 行。data: RawData<M>
似乎有问题,但是,parseXml()
也返回此类型,我们可以在第 53 行看到 new XmlStats(['b']).parseXml("<xml>");
返回类型:
const xml: {
orig_b: number;
}
所以这似乎有效......但是,在方法 parseModules()
中,我们得到一个错误:
Argument of type 'UnionToIntersection<UnionArgTypes<SelectedModules<M>>>' is not assignable to parameter of type '{ orig_a: string; } & { orig_b: number; }'.
Type 'unknown' is not assignable to type '{ orig_a: string; } & { orig_b: number; }'.
Type 'unknown' is not assignable to type '{ orig_a: string; }'.
Type 'UnionToIntersection<UnionArgTypes<SelectedModules<M>>>' is not assignable to type '{ orig_a: string; }'.
Type 'unknown' is not assignable to type '{ orig_a: string; }'.(2345)
这对我来说似乎是一个错误,但我不能 100% 确定这是我尝试编写的一些最复杂的 TS。
我真的很感激这方面的帮助。谢谢!