未能使用cxml生成的TypeScript接口

时间:2016-06-09 18:33:48

标签: typescript xsd xml-parsing

我正在使用TypeScript为XML REST服务实现API包装器。所有请求和响应都在模式中描述,我能够使用cxsdcxml生成适当的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%兼容。

1 个答案:

答案 0 :(得分:1)

如果这是您想要的代码

let obj: Foo = {
    a: "Test1",
    b: {
        c: "Test2"
    }
}

这是您需要的类型定义

interface Foo {
  a: string,
  b: {
    c: string
  }
}
  

此处使用TypeScript的全部想法是验证它们与原始XSD架构100%兼容

你必须自己写。或者找一个工具(我觉得它不符合您的质量要求,也就是工作。)

相关问题