using external module in protractor test - cannot use namespace as a type

喜欢而已 提交于 2020-01-05 04:36:10

问题


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 bymodule.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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!