【问题标题】:quicktype json schema to typescript just-type in custom functionquicktype json schema to typescript just-type in custom function
【发布时间】:2020-07-22 07:18:41
【问题描述】:

我有一个使用 quicktype API 的函数。它可以工作,但我想使用“just-types”选项来只保留打字稿接口(没有类和函数)。

这是我的代码:

async function* getFiles(dir) {
  const dirents = await readdir(dir, { withFileTypes: true });
  for (const dirent of dirents) {
    const res = resolve(dir, dirent.name);
    if (dirent.isDirectory()) {
      yield* getFiles(res);
    } else {
      yield res;
    }
  }
}

(async () => {
  const directoryPath = join(__dirname, "XML")

  for await (const file of getFiles(directoryPath)) {
    const filename = basename(file);
    const filePathArray = file.split('\\');
    const foldername = filePathArray[filePathArray.length - 2];

    if (!fs.existsSync(join(__dirname, 'interfaces', foldername))){
      fs.mkdirSync(join(__dirname, 'interfaces', foldername));
    }
    const interfaceName = filename.split('.')[0];

    const schemaInput = new JSONSchemaInput(new q.JSONSchemaStore());
    schemaInput.addSource({ name: interfaceName, schema: fs.readFileSync(file, "utf8")});

    const inputData = new InputData();
    inputData.addInput(schemaInput);

    quicktype({inputData, lang: new TypeScriptTargetLanguage()}).then(function (data) {
      fs.writeFile(join(__dirname, 'interfaces', foldername, interfaceName + '.ts'), data.lines.join('\n'), function () {});
    });
  }
})()

我见过 TypeScriptRenderer 类,它可以将布尔值“justTypes”作为第三个参数,但我不知道如何使它起作用。

我已尝试传递所有参数:

TypeScriptRenderer(new TypeScriptTargetLanguage(), ??, { justTypes: true });

第二个参数是 RenderContext。我不知道如何实例化它。

有人知道如何让它工作吗?

【问题讨论】:

    标签: quicktype


    【解决方案1】:

    如果其他人偶然发现此问题正在寻找类似的答案,您可以这样做

    quicktype({ lang: new TypeScriptTargetLanguage(), inputData, rendererOptions: { 'just-types': 'true' } });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-02
      • 2022-07-31
      • 2015-05-13
      • 2022-12-02
      • 1970-01-01
      • 1970-01-01
      • 2020-02-21
      • 2022-12-26
      相关资源
      最近更新 更多