为JSON生成接口

时间:2015-11-27 02:19:16

标签: typescript

我希望能够使用TypeScript来分析JSON对象并输出接口。

我尝试动态的目的是转换JSON,如下所示:

{
  "foo": "some string value",
  "bar": 5,
  "baz": {
    "a": "string"
  },
  "baz2": {
    "a": "string"
  }
}

调用可能存在或不存在的此类函数:

typescript.doSomeMagic(objFromAbove);
// or
typescript.doSomeMagic(JSON.stringify(objFromAbove));

让它输出一个接口(或接口):

interface IProvidedName {
  foo: string;
  bar: number;
  baz: { a: string; b: number; }
  baz2: IBaz2;
}
interface IBaz2 {
  a: string;
}

我可以使用baz或上面baz2的格式。

我目前正在挖掘寻找对象推理函数的typescript模块。

1 个答案:

答案 0 :(得分:3)

  

我正在挖掘寻找对象推理函数的typescript模块。

有一个工具json2dts:http://xperiments.in/json2dts/

它也被整合到atom-typescript中:https://github.com/TypeStrong/atom-typescript/pull/466