我试图将我的项目分成两个单独的项目,然后在它们之间重用文件。现在,我将我的Helper类用作外部模块,尝试在测试中使用它时遇到错误。我导入/导出模块不正确吗?
- Cannot use namespace 'Helper' as a type
- Cannot use 'new' with an expression whose type lacks a call or construct signature.
主要项目
test.ts
import Helper = require('sub-project');
describe(`Test Description`, () => {
let helper: Helper; // error - cannot use namespace Helper as a type
before(async () => {
helper = new Helper(); // error - Cannot use 'new' with an expression whose type lacks a call or construct signature.
await helper.myFunction(xx, xx);
});
package.json
"devDependencies": {
"sub-project": "file:../../sub-project/e2e"
}
子项目
app.ts
export {Helper} from './src/xx/helper’;
helper.ts
export class Helper {
}
package.json
"name": "sub-project",
"main": "app.ts"
tsconfig.json
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "lib",
"rootDir": ".",
"target": "es5",
"module": "commonjs",
"types": [
],
}
}
答案 0 :(得分:0)
import Helper = require('sub-project');
与import { Helper } from ...
不同。在helper文件中,您要导出的名称为Helper
的类Helper(不是默认值,不是绝对的),因此在test.ts文件中,您应该通过import { Helper } from '..'
如果使用export default 42
,则应使用import magicNumber from ...
(不使用{})。
您还可以在'lib'. It's used when someone is exporting all things by
module.exports = ...`中找到类似'import * as Lib的语法。如果您不需要
我也建议不要在TS文件中使用require
语法,import
应该可以更好地解析类型,并且可读性更好。