【发布时间】:2016-06-09 18:33:48
【问题描述】:
我正在使用 TypeScript 为 XML REST 服务实现 API 包装器。所有请求和响应都在模式中描述,我能够使用 cxsd 和 cxml 生成适当的 TypeScript 和 JS 定义。但是我对cxsd生成的代码感到困惑。例子是in the repo,这里是简化版:
interface _foo {
a: string;
b: bar;
}
interface foo extends _foo { constructor: { new(): foo } }
var foo: { new(): foo };
interface _bar {
c: string;
}
interface bar extends _bar { constructor: { new(): bar } }
var bar: { new(): bar };
constructor: { new(): foo } 到底是什么意思?
我需要自己实际创建这些对象来构建 XML 并将其发送到 API 后端,但我无法做到这一点。尝试类似:
let obj: foo = {
a: "Test1",
b: {
c: "Test2"
}
}
我收到此错误:
scripts/foo.ts (13,5): Type '{ a: string; b: { c: string; }; }' is not assignable to type 'foo'.
Types of property 'constructor' are incompatible.
Type 'Function' is not assignable to type 'new () => foo'.
Type 'Function' provides no match for the signature 'new (): foo' (2322)
如何创建这些对象?在这里使用 TypeScript 的整个想法是验证它们与原始 XSD 架构 100% 兼容。
【问题讨论】:
标签: typescript xsd xml-parsing