是否从单独的目录导入typescript模块而不导航相对路径?

是否从单独的目录导入typescript模块而不导航相对路径?,typescript,typescript2.0,Typescript,Typescript2.0,我有以下这个类型脚本文件src/main/ts/hello.ts包含: export function hello() { return 'Hello World!'; } export default hello; 相应的测试在src/test/ts/hello.spec.ts中。它像这样导入hello: import hello from 'hello'; IIUC下面的配置应该允许我在测试中导入hello.ts,而不使用相对路径 {

我有以下这个类型脚本文件
src/main/ts/hello.ts
包含:

    export function hello() {
      return 'Hello World!';
    }

    export default hello;
相应的测试在
src/test/ts/hello.spec.ts
中。它像这样导入
hello

    import hello from 'hello';
IIUC下面的配置应该允许我在测试中导入
hello.ts
,而不使用相对路径

    {
        "include": [
            "src/main/ts/**/*.ts"
        ],
        "exclude": [
            "node_modules"
        ],
        "baseUrl": ".",
        "paths": {
            "*": [
                "*", "src/main/ts/*"
            ]
        },
        "compilerOptions": {
            "experimentalDecorators": true,
            "noImplicitAny": true,
            "moduleResolution": "node",
            "target": "es6"
        }
    }
但是,我仍然会遇到以下错误:

src/test/ts/hello.spec.ts(1,19): error TS2307: Cannot find module 'hello'.

想法?

您可以在
tsconfig.json
中指定
baseUrl
path
属性:

{
   "include": [
     "src/main/ts/**/*.ts",
     "src/test/ts/**/*.ts"
   ],
   "exclude": [
     "node_modules"
   ],

   "compilerOptions": {
     "experimentalDecorators": true,
     "noImplicitAny": true,
     "moduleResolution": "node",
     "target": "es6",
     "baseUrl": ".",
     "paths": {
       "*": [
         "*", "src/main/ts/*"
       ]
     }
   }
 }

文档:

Hi,@Ole,你们应该检查TS模块Hi@Saravana-我用我得到的结果更新了这个问题。看起来我做错了什么吗?请查看我的最新答案。1. <代码>基本URL和路径应位于
编译器选项
内。2.您的
include
缺少测试文件的路径。