【问题标题】:Failing to use the TypeScript interfaces generated by cxml无法使用 cxml 生成的 TypeScript 接口
【发布时间】:2016-06-09 18:33:48
【问题描述】:

我正在使用 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% 兼容。

【问题讨论】:

    标签: typescript xsd xml-parsing


    【解决方案1】:

    如果这是你想要的代码

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

    这是你需要的类型定义

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

    在这里使用 TypeScript 的整个想法是验证它们与原始 XSD 架构 100% 兼容

    你必须自己写。或者找到一种工具(我认为不存在满足您的质量要求的工具,即有效。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-01
      • 2022-01-21
      • 2012-12-07
      • 2019-05-03
      • 2013-01-24
      • 2020-06-24
      • 2023-03-31
      • 2019-09-24
      相关资源
      最近更新 更多