简体   繁体   English

从 typescript 中的索引文件导出 JSON 文件

[英]export JSON file from index file in typescript

in my typescript project, i am able to re-export my default exports in index.ts of a folder.在我的 typescript 项目中,我能够在文件夹的 index.ts 中重新导出我的默认导出。

for eg:例如:

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

but i am unable to export JSON files like this.但我无法像这样导出 JSON 文件。

export * as configFiles from "./config";

how to bundle and export JSON files such that i can use it in paths of tsconfig如何捆绑和导出 JSON 文件,以便我可以在 tsconfig 的路径中使用它

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

Add the following options inside your tsconfig.json file:tsconfig.json文件中添加以下选项:

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

After updating tsconfig file export configFile like below:更新tsconfig文件后导出 configFile,如下所示:

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

Now to import the configFiles inside your code use below:现在要在您的代码中configFiles配置文件,请使用以下代码:

import { configFiles } from "./file-path-to-configFiles-variable";

console.log(configFiles);

example tsconfig.json示例tsconfig.json

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM