[英]import JavaScript module in angular nx project
I have an angular project built with nx I want to import standard JavaScript libraries which do not have typescript typings for example I want to install and use cytoscape and cytoscape-context-menus我有一个用nx构建的 angular 项目我想导入没有打字稿类型的标准 JavaScript 库,例如我想安装和使用cytoscape和cytoscape-context-menus
I get compilation errors such as我收到编译错误,例如
error TS7016: Could not find a declaration file for module 'cytoscape-context-menus'.错误 TS7016:找不到模块“cytoscape-context-menus”的声明文件。 'C:/dev/armo/armo-ng-app/node_modules/cytoscape-context-menus/cytoscape-context-menus.js' implicitly has an 'any' type. 'C:/dev/armo/armo-ng-app/node_modules/cytoscape-context-menus/cytoscape-context-menus.js' 隐含地有一个 'any' 类型。 Try
npm i --save-dev @types/cytoscape-context-menus
if it exists or add a new declaration (.d.ts) file containingdeclare module 'cytoscape-context-menus';
尝试npm i --save-dev @types/cytoscape-context-menus
如果它存在或添加一个包含declare module 'cytoscape-context-menus';
的新声明 (.d.ts) 文件declare module 'cytoscape-context-menus';
How can I solve them?我该如何解决它们? I tried to put declaration files but I think my setup doesn't see them.我试图放置声明文件,但我认为我的设置没有看到它们。
since there's no available type definition npm package, you have to write your own.由于没有可用的类型定义 npm 包,您必须自己编写。 start with the ones you need and continue to build on top of it as needed.从您需要的开始,并根据需要继续在其基础上构建。
if you like you can share it too as an npm package.如果您愿意,也可以将其作为 npm 包共享。
found this article for writing TypeScript type definition files by @dubtypescript: http://blog.wolksoftware.com/contributing-to-definitelytyped通过@dubtypescript 找到了这篇关于编写 TypeScript 类型定义文件的文章: http ://blog.wolksoftware.com/contributing-to-definelytyped
also take a look at how cytoscope is typed from here, borrow their ways: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/cytoscape/index.d.ts也看看细胞镜是如何从这里输入的,借用他们的方式: https : //github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/cytoscape/index.d.ts
Added a stackblitz to checkout:添加了一个 stackblitz 到结帐:
https://stackblitz.com/edit/cytoscape-6cyuem?file=src%2Fapp%2Fcytoscape-context-menus.d.ts https://stackblitz.com/edit/cytoscape-6cyuem?file=src%2Fapp%2Fcytoscape-context-menus.d.ts
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.