对于项目d.ts文件中的名称,使用tsc但不使用Webpack时出错TS2304

时间:2017-07-28 07:15:21

标签: typescript webpack tsc

问题

如果我输入tsc main-test.ts --noEmit,则会显示许多TS2304: Cannot find name TestInfo错误消息。它找不到的名称是我在项目目录中创建的d.ts文件中声明的接口,例如下面的testGlobals.d.ts

如果我使用Webpack和awesome-typescript-loader(node_modules\.bin\webpack --config wtest.webpack.config.js)进行编译,则不会出现这些消息 - 项目编译成功。

不幸的是,即使在CI服务器上运行的脚本是由webpack编译的,Travis CI上也会出现相同的错误。我如何摆脱这些错误?


我试过

除了tsconfig.json之外,还将项目源目录添加到node_modules/@types中的typeRoots。


相关文件

wtest.webpack.config.js

var path = require('path');
var webpack = require('webpack');


var entryObj = {
    'test-script': path.join(__dirname, '/test/main-test.ts')
};
var manifestFile = '/test/manifest.json';
var outdir = '/dist/test';
var moduleRulesInclude = [path.join(__dirname, 'src'), path.join(__dirname, 'test')];
var devtool = 'cheap-source-map';

module.exports = {
    devtool: devtool,
    entry: entryObj,
    resolve: {
        extensions: ['.js', '.ts', '.tsx']
    },
    module: {
        rules: [{
            test: /\.tsx?$/,
            loaders: ['awesome-typescript-loader'],
            include: moduleRulesInclude
        }]
    },
    // Workaround for `can't resolve module 'fs'` issue
    node: {
        fs: 'empty',
        module: false
    }
};

tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "declaration": false,
    "noImplicitAny": false,
    "removeComments": true,
    "experimentalDecorators": true,
    "noLib": false,
    "jsx": "react",
    "outDir": "dist"
  },
  "exclude": [
    "node_modules",
    "static"
  ]
}

testGlobals.d.ts

interface TestInfo {
    testName: string;
    url: string;
    title: string;
    author: string;
    chapterUrls: string[];
    parser: ParserReturner;
}

0 个答案:

没有答案