问题
I am trying to split my project into two separate projects and then reuse files between them. Now that I am using my Helper class as an external module, I am getting errors trying to use it in my test. Am i importing/exporting the module incorrectly?
- Cannot use namespace 'Helper' as a type
- Cannot use 'new' with an expression whose type lacks a call or construct signature.
main project
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"
}
sub-project
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": [
],
}
}
回答1:
import Helper = require('sub-project');
is not the same as import { Helper } from ...
. In helper file you're exporting class Helper with Helper
name (not default, not absolute) so in test.ts file you should import it by import { Helper } from '..'
In case of export default 42
you should use import magicNumber from ...
(without {}).
Also you can find syntax like this 'import * as Lib from 'lib'. It's used when someone is exporting all things by
module.exports = ...`. Don't use that if you don't need to
Also i suggest to don't use require
syntax in TS files, import
should better resolved types and also is more readable.
来源:https://stackoverflow.com/questions/55297763/using-external-module-in-protractor-test-cannot-use-namespace-as-a-type