如果我输入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;
}