简体   繁体   English

如何在打字稿中导入类型定义?

[英]How to import the type-definition in typescript?

I tried use the type-definition for hammerjs. 我尝试为Hammerjs使用类型定义。 As: 如:

import Hammer from 'hammerjs';

But i get this error: 但是我得到这个错误:

Error TS2307: Cannot find module 'hammerjs' 错误TS2307:找不到模块'hammerjs'

I have two question. 我有两个问题。 First, is necessary get all files of tile definition in github, our i can get the simples definition for hammerjs? 首先,必须在github中获取tile定义的所有文件,我们可以为Hammerjs获取简单定义吗?

My package.json contains: 我的package.json包含:

"dependencies": {
   "git+https://git@github.com/DefinitelyTyped/DefinitelyTyped.git"
}

The second question, whats my error about import module? 第二个问题,关于导入模块的错误是什么?

I resolve the problem after read: http://x-team.com/2016/06/include-javascript-libraries-in-an-ionic-2-typescript-project/ 阅读后,我解决了问题: http : //x-team.com/2016/06/include-javascript-libraries-in-an-ionic-2-typescript-project/

I installed hammerjs with the command: 我使用以下命令安装了Hammerjs:

typings install github:DefinitelyTyped/DefinitelyTyped/hammerjs/hammerjs.d.ts#de8e80dfe5360fef44d00c41257d5ef37add000a --global --save 键入安装github:DefinitelyTyped / DefinitelyTyped / hammerjs / hammerjs.d.ts#de8e80dfe5360fef44d00c41257d5ef37add000a --global --save

Then still appeared the error: 然后仍然出现错误:

Error TS2307: Cannot find module 'hammerjs' 错误TS2307:找不到模块'hammerjs'

I am development the app with ionic2, i found that the compiler of typescript look the file main.d.ts and not index.d.ts . 我正在使用ionic2开发该应用程序,发现打字稿的编译器查找文件main.d.ts而不是index.d.ts After rename the file index.d.ts to main.d.ts and works fine! 将文件index.d.ts重命名为main.d.ts后,可以正常工作! The file main.d.ts found in root-your-app/typings 在root-your-app / typings中找到文件main.d.ts

For import in the project i use: import * as Hammer from 'hammerjs'; 对于项目导入,我使用: import * as Hammer from'hammerjs';

Obviously you misuse the package.json dependencies. 显然,您滥用了package.json依赖项。

Please read at https://github.com/DefinitelyTyped/DefinitelyTyped#how-to-get-the-definitions how to use the definition provided there, or at https://www.npmjs.com/package/typings how to use the newer 'typings' definitions. 请阅读https://github.com/DefinitelyTyped/DefinitelyTyped#how-to-get-the-definitions如何使用此处提供的定义,或阅读https://www.npmjs.com/package/typings如何使用较新的“类型”定义。

To answer your first question, you will only need the definition for your project dependencies. 要回答第一个问题,您仅需要项目依赖项的定义。

To answer your second question, the typescript compiler attempt to load the hammerjs dependency but cannot find it. 为了回答您的第二个问题,打字稿编译器尝试加载Hammerjs依赖项,但找不到它。 You have to add it as a dependency in your package.json and install it using npm. 您必须将其添加为package.json中的依赖项,并使用npm进行安装。

Hammerjs may not be the same as chrome extention typings I was dealing with but issue sounds very much like what I was experiencing when you are missing an import or export in the module file you will need to give a reference: Hammerjs可能与我正在处理的chrome扩展类型不同,但是问题听起来很像我在模块文件中缺少导入或导出时遇到的情况,您需要提供参考:

Step 1: npm install --save @types/chrome 步骤1:npm install --save @ types / chrome

Step 2: Add /// to code needing to use modules 步骤2:向需要使用模块的代码添加///

Step 3: that file can access all the declared modules.[do not import chrome or '@types/chrome'] https://typescript.codeplex.com/wikipage?title=Modules%20in%20TypeScript 步骤3:该文件可以访问所有声明的模块。[不导入chrome或'@ types / chrome'] https://typescript.codeplex.com/wikipage?title=Modules%20in%20TypeScript

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM