如何在没有<reference>的情况下在TypeScript中使用'Typings'导入模块?

oli*_*ost 5 jasmine typescript protractor typescript-typings

我真的试图写有测试量角器茉莉花打字稿.TSD现已弃用,所以我必须使用'Typings'来操作TypeScript定义.所以我安装了它:

npm install typings -g
Run Code Online (Sandbox Code Playgroud)

然后我用它来安装像这样的Jasmine和Chance定义:

typings install jasmine --source dt --save –-global
typings install chance--source dt --save –-global
Run Code Online (Sandbox Code Playgroud)

我还添加了"files"部分并排除了'node_modules':

// tsconfig.json
{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "declaration": false,
    "noImplicitAny": false,
    "outDir": "tmp",
    "types": ["node", "jasmine", "chance"]
  },
  "files": [
    "typings/index.d.ts"
  ],
  "include": [
    "lib",
    "specs"
  ],
  "exclude": [
    "node_modules"
  ]
}
Run Code Online (Sandbox Code Playgroud)

问题是WebStorm和Visual Code studio都找不到所有Jasmine方法和'机会'模块的定义.错误是:

**“TS2304: Cannot find name 'describe'”** 
**“TS2307: Cannot find module 'chance'”**
Run Code Online (Sandbox Code Playgroud)

分别.这是我的spec文件:

在此输入图像描述

在'typings /'文件夹中,我看到了TypeScript定义的引用链接:

文件夹结构和typings/index.d.ts内容

我错过了什么吗?为什么我的IDE无法找到Jasmine和Chance的定义?

PS测试在转移到Javacript后工作.

添加后,PSS错误将消失

///<reference path="###"/>
Run Code Online (Sandbox Code Playgroud)

到spec文件.但我不想用它.

Protractor: 4.0.9;
TypeScript 2.0.3;
Typings 1.5.0;
Run Code Online (Sandbox Code Playgroud)

提前致谢!

dta*_*enc 7

有一些事情阻碍了事情的顺利运作.首先,如果您使用的是Typescript 2.0,那么typings也会被弃用.

Typescript 2.0支持节点模块中的打包输入(通常安装方式npm install @types/module-name.明确键入的所有声明都可以作为"@ types/*"命名空间下的npm模块使用,所以如果你想要离开tsd,typings不应该是你的最终目的地.

话虽这么说,typescript 2.0仍然适用于打字,所以让我指出我在配置文件中看到的一些问题.

类型属性

{ 
    "types": ["node", "jasmine", "chance"]  
}
Run Code Online (Sandbox Code Playgroud)

类型,属性仅用于新的基于npm包的输入@types/*.既然你还没有使用它,你不应该拥有这个属性tsconfig.json.(有关此类和相关属性的详细说明,请参阅TypeScript 2:无类型npm模块的自定义类型.

文件/包含/排除属性

{
 "files": [
    "typings/index.d.ts"
  ],
  "include": [
    "lib",
    "specs"
  ],
  "exclude": [
    "node_modules"
  ]
}
Run Code Online (Sandbox Code Playgroud)

虽然您可以选择同时使用这两个文件并包含属性,但这不是必需的.您可以将数组中的typings/index.d.ts引用移动到files数组中include array,然后消除files.

此外,您遇到的最大问题是您的include语法错误.include采用glob模式.你不能只是简单地放置文件夹名称,但需要通过递归模式跟随它们:

{
  "include": [
    "typings/index.d.ts",
    "lib/**/*",
    "specs/**/*"
  ],
}
Run Code Online (Sandbox Code Playgroud)

最后一件事是你不需要排除node_modules.只有在排除与您的包含模式匹配的内容时才需要排除.由于node_modules您的include模式不匹配,因此无需将其排除.

我认为,如果你做出这些微小的改变,事情应该按照你的预期运作,但同样,我会真正考虑一直走向新的@types/*基础类型,因为它们更容易管理,更方便,不需要外部工具(除了npm),如typings或tsd.