TypeScript:编译增加项目结构深度时如何从父文件夹导入

TypeScript: How to import from parent folder when compilation adds depth to project structure

我的项目结构如下

dist
  |- src
  |   |- s.js
  |- test
      |- t.js
node_modules
results
  |- r.json
src
  |- s.ts
test
  |- t.ts
tsconfig.json
typings.d.ts

s.ts 导入 r.json 如下:

import * as results from '../results/r.json';

显然,这在编译后不起作用,因为 results 现在是两个文件夹...

解决这个问题的正确方法是什么?下面是我的tsconfig.json

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "inlineSourceMap": true,
    "lib": [
      "dom",
      "es2015"
    ],
    "moduleResolution": "node",
    "outDir": "dist/",
    "target": "es5",
    "typeRoots": [
      "node_modules/@types",
      "./typings.d.ts"
    ]
  },
  "include": [
    "src/**/*.ts",
    "results/r.json",
    "test/**/*.ts",
    "./typings.d.ts"
  ],
  "exclude": [
    "node_modules"
  ],
  "compileOnSave": false,
  "atom": {
    "rewriteTsconfig": false
  }
}

最好的办法就是完全避免这种情况。尝试使深度级别匹配。

例如:

root
  dist-es5
    index.ts
    index.spec.ts
  src
    index.ts
    index.spec.ts

至于防止 test/spec 代码被分发,您可以有多个 tsconfig.json 例如你可以看看: