【问题标题】:export JSON file from index file in typescript从打字稿中的索引文件导出JSON文件
【发布时间】:2021-10-31 20:48:33
【问题描述】:

在我的打字稿项目中,我可以在文件夹的 index.ts 中重新导出我的默认导出。

例如:

export { default as BaseError } from "./errorResponse";

但我无法像这样导出 JSON 文件。

export * as configFiles from "./config";

如何捆绑和导出 JSON 文件,以便我可以在 tsconfig 的路径中使用它

{
  "compilerOptions": {
    "paths": {
      "@errorClass": ["src/helpers/errorClasses"],
      "@config": ["src/config"],
      "@routes": ["src/routes"]
    },
}

【问题讨论】:

    标签: javascript node.js json typescript


    【解决方案1】:

    示例 tsconfig.json

    "compilerOptions": {
            "baseUrl": "src",
            ...
            "paths": {
                "@config":["src/config/*"],
             ...
            },`
    
    import { xConfig } from '@config/index';
    

    【讨论】:

      【解决方案2】:

      在您的 tsconfig.json 文件中添加以下选项:

      {
        "compilerOptions": {
          "esModuleInterop": true,
          "resolveJsonModule": true
        }
      }
      

      更新tsconfig文件后导出configFile如下:

      export { default as configFiles } from "./config.json";
      

      现在在您的代码中导入configFiles,使用以下代码:

      import { configFiles } from "./file-path-to-configFiles-variable";
      
      console.log(configFiles);
      

      【讨论】:

        猜你喜欢
        • 2020-11-23
        • 2019-06-04
        • 1970-01-01
        • 2020-06-30
        • 1970-01-01
        • 2019-01-03
        • 2016-08-18
        • 2021-08-14
        • 2020-08-27
        相关资源
        最近更新 更多