【问题标题】:How can I create human-readable JSON from TypeScript CompilerOptions?如何从 TypeScript CompilerOptions 创建人类可读的 JSON?
【发布时间】:2020-02-07 19:14:47
【问题描述】:

给定一个像这样的对象:

import * as ts from "typescript";

const options: ts.CompilerOptions = {
    target: ts.ScriptTarget.ESNext,
    moduleResolution: ts.ModuleResolutionKind.NodeJs
}

如何生成这样的人类可读的 tsconfig.json?

{
    "target": "ESNext",
    "moduleResolution": "NodeJs"
}

或者,如果有另一种方法(从人类可读的 tsconfig 输入解析 ts.CompilerOptions 对象),我可以解决我的问题。

我问这个问题是因为我试图使用同一个对象来生成一个 tsconfig.json 和一个对象以提供给 TS Compiler API。

【问题讨论】:

    标签: typescript typescript-compiler-api


    【解决方案1】:

    没有办法!

    但鉴于您要解决的问题,看起来有解决方案:

    或者,如果有另一种方法(从人类可读的 tsconfig 输入解析 ts.CompilerOptions 对象),我可以解决我的问题。

    我问这个问题是因为我试图使用同一个对象来生成一个 tsconfig.json 和一个对象以提供给 TS Compiler API。

    您可以从 JSON 样式的配置对象开始,其中没有任何值是枚举,并使用它来生成 tsconfig.json。

    然后使用ts.convertCompilerOptionsFromJson 将编译器选项从JSON 样式对象转换为ts.CompilerOptions

    例如,这里是how gulp-typescript does the conversion

    import * as ts from "typescript";
    
    // later in the file
    const settingsResult = typescript.convertCompilerOptionsFromJson(settings, projectDirectory);
    
    if (settingsResult.errors) {
        reportErrors(settingsResult.errors, typescript);
    }
    

    【讨论】:

      猜你喜欢
      • 2019-04-28
      • 2013-12-14
      • 2021-12-06
      • 1970-01-01
      • 1970-01-01
      • 2012-06-18
      • 1970-01-01
      • 2016-05-07
      • 1970-01-01
      相关资源
      最近更新 更多