在量角器测试中使用外部模块 - 不能使用命名空间作为类型 [英] using external module in protractor test - cannot use namespace as a type

查看:69
本文介绍了在量角器测试中使用外部模块 - 不能使用命名空间作为类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的项目拆分为两个单独的项目,然后在它们之间重复使用文件.现在,我将 Helper 类用作外部模块,尝试在测试中使用它时出现错误.我是否错误地导入/导出模块?

 - 不能使用命名空间Helper"作为类型- 不能将 'new' 用于类型缺少调用或构造签名的表达式.

<块引用>

主要项目

test.ts

import Helper = require('sub-project');描述(`测试描述`,()=> {让帮手:帮手;//错误 - 不能使用命名空间助手作为类型之前(异步()=> {帮手 = 新帮手();//错误 - 不能将new"用于类型缺少调用或构造签名的表达式.等待 helper.myFunction(xx, xx);});

package.json

"devDependencies": {子项目":文件:../../sub-project/e2e"}

<块引用>

子项目

app.ts

 export {Helper} from './src/xx/helper';

helper.ts

export class Helper {}

package.json

 "name": "子项目","main": "app.ts"

tsconfig.json

<代码>{"extends": "../tsconfig.json",编译器选项":{"outDir": "lib","rootDir": ".","目标": "es5","module": "commonjs",类型":[],}}

解决方案

import Helper = require('sub-project');import { Helper } from 不同....在帮助文件中,您使用 Helper 名称(不是默认值,不是绝对)导出类 Helper,因此在 test.ts 文件中,您应该通过 import { Helper } from '..'

export default 42的情况下,你应该使用import magicNumber from ...(没有{}).

您还可以找到类似这样的语法 'import * as Lib from 'lib'.当有人通过module.exports = ...`导出所有东西时使用它.如果您不需要,请不要使用它

另外我建议不要在 TS 文件中使用 require 语法,import 应该更好地解析类型并且更易读.

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": [
        ],
    }
}

解决方案

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.

这篇关于在量角器测试中使用外部模块 - 不能使用命名空间作为类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆