Visual Studio TypeScript - 找不到模块

时间:2017-06-19 13:23:58

标签: visual-studio typescript module typescript-typings

我正在使用Visual Studio Professional 2015并拥有Angular 4,TypeScript和Webpack应用程序。

我们希望能够从目录和文件中import,因此,例如,我们有一个名为core的文件夹,在此文件夹中有许多不同的组件,并且也在在该文件夹中,我们有一个index.ts文件,用于导出文件夹中每个文件的所有内容。因此,例如,在核心文件夹中有一个名为core.services.utils.ts的组件:

export class Utils {
    static isFunction(test: any): boolean {
        return typeof test === 'function';
    }
}

然后在index.ts文件中我们有:

export { Utils } from './core.services.utils';

在我们的Webpack配置中,我们在resolve config:

中有一个别名
alias: {
    '@product/core': path.join(__dirname, './Features/core')
}

现在,在其他模块中,我们可以直接从别名导入,例如

import { Utils } from '@product/core';

当我们通过Webpack构建时,一切都很好,应用程序按预期工作。遗憾的是,Visual Studio无法在Webpack配置中看到别名,因此当我们通过Visual Studio构建时,它会出现如下错误:

  

构建:找不到模块'@ product / core'

如何告诉Visual Studio这个别名?我是否需要在index.d.ts文件旁边添加index.ts文件?如果是这样,该文件应该是什么?

1 个答案:

答案 0 :(得分:1)

我可以使用paths文件中的tsconfig.json配置来解决此问题,例如:

"paths": {
    "@product/core": [ "Shared/Features/core/index" ]
}

我不想为每个要导入的文件夹设置单独的路径,但我很高兴它已删除了构建错误。