简体   繁体   English

导入C3和D3时Typescript编译错误

[英]Typescript compile errors when importing C3 and D3

I am playing with the graphing library C3 in an Ionic2/Angular2 typescript project. 我正在Ionic2 / Angular2打字稿项目中使用图形库C3 I have installed C3 via npm , and the type definitions via tsd . 我已经通过npm安装了C3 ,并通过tsd安装了类型定义。

I have imported when into my own ts file as follows.. 我已经将以下内容导入到自己的ts文件中了:

import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import * as C3 from 'c3';
import * as D3 from 'd3';

@Component({
   templateUrl: 'build/pages/graphs-page/graphs-page.html'
 ....
})

All appears fine. 一切都很好。 I can see the typings for the C3 (and dependant D3), and also when I run everything seems to work. 我可以看到C3(和从属D3)的类型,而且在我运行时,一切似乎都可以正常工作。

However, when the application builds (when I run ionic serve ), I always get the following typescript compile errors... 但是,在构建应用程序时(当我运行ionic serve ),我总是会收到以下打字稿编译错误...

TypeScript error: D:/dev/ionic2/testcomonents/app/pages/graphs-page/graphs-page.ts(3,21): Error TS2307: Cannot find module 'c3'.
TypeScript error: D:/dev/ionic2/testcomonents/app/pages/graphs-page/graphs-page.ts(4,21): Error TS2307: Cannot find module 'd3'.

Does anyone have any ideas why I am getting these errors at build time, when I get no errors in the IDE (I am using vscode ), and everything seems to work fine? 有谁知道为什么在构建时会出现这些错误,而我在IDE中没有错误(我正在使用vscode ),并且一切似乎都正常工作?

[EDIT] [编辑]

I have since installed typescipt 2 and run with the --traceResolution flag. 自从我安装了typecipt 2并使用--traceResolution标志运行。 I can see that tsc only seems to look under various levels of node_modules and never looks under typings folder, which is where vscode is looking. 我可以看到, tsc似乎只在各级寻找node_modules从不下看起来typings的文件夹,这就是vscode期待。

More confusing (to me) is how the c3.js source is included, when this is located under the node_modules/c3 folder. (对我来说)更令人困惑的是,当c3.js源代码位于node_modules / c3文件夹下时,它是如何包含的。 I have not specifically added any reference to c3.js , yet the graphs show up. 我没有专门添加对c3.js任何引用,但是显示了图表。

The settings in the tsconfig.json are tsconfig.json中的设置是

{
   "compilerOptions": {
   "target": "es5",
   "module": "commonjs",
   "emitDecoratorMetadata": true,
   "experimentalDecorators": true
 },

"filesGlob": [
   "**/*.ts",
   "!node_modules/**/*"
 ],
 "exclude": [
   "node_modules",
   "typings/main",
   "typings/main.d.ts"
  ],
 "compileOnSave": false,
 "atom": {
   "rewriteTsconfig": false
 }
}

I have tried various edits in the above tsconfig.json but cannot get it to look in the typings folder. 我在上面的tsconfig.json尝试了各种编辑,但无法在tsconfig.json文件夹中查看。

So my questions now become 所以我的问题现在变成

  1. How to make Typscript look in the typings folder 如何在打字文件夹中使打字稿看起来
  2. How does the the actual c3.js file located at node_modules\\c3\\c3.js get included in the Ionic bundle (since I have not added a reference to it anywhere) 如何将位于node_modules \\ c3 \\ c3.js上的实际c3.js文件包含在Ionic捆绑包中(因为我没有在任何地方添加对它的引用)

I think I now know the answer to this, but if I am incorrect, please correct me! 我想我现在知道答案了,但是如果我不正确,请纠正我!

looking in the Ionic file D:\\dev\\ionic2\\testcomponents\\node_modules\\ionic-gulp-browserify-typescript\\index.js , there is the following src property 在Ionic文件D:\\dev\\ionic2\\testcomponents\\node_modules\\ionic-gulp-browserify-typescript\\index.js ,有以下src属性

var defaultOptions = {
   watch: false,
   src: ['./app/app.ts', './typings/main.d.ts'],

so If I add the following to main.d.ts 所以如果我将以下内容添加到main.d.ts

/// <reference path="globals/c3/index.d.ts" />
/// <reference path="globals/d3/index.d.ts" />

the errors go away. 错误消失了。

If I run tsc --traceResolution I still see errors, but my guess here is that running tsc from the command line does not include looking at the above Ionic script or main.d.ts . 如果我运行tsc --traceResolution我仍然看到错误,但是我的猜测是从命令行运行tsc不包括查看上面的Ionic脚本或main.d.ts

To get rid of the error when using tsc --traceResolution , I tried added the following to the file D:\\dev\\ionic2\\testcomponents\\node_modules\\c3\\package.json 为了摆脱使用tsc --traceResolution时的错误,我尝试将以下内容添加到文件D:\\dev\\ionic2\\testcomponents\\node_modules\\c3\\package.json

"typings": "../../typings/globals/c3/index.d.ts",

and then do the same thing for d3 . 然后对d3做同样的事情。 But this then causes the following error when I run the Ionic build 但是当我运行Ionic版本时,这会导致以下错误

TypeScript error: d:/dev/ionic2/testcomponents/app/pages/graphs-page/graphs-page.ts(3,21): Error TS2656: Exported external package
typings file 'd:/dev/ionic2/testcomponents/typings/globals/c3/index.d.ts' is     not a module. Please contact the package author to update the package  definition.  

So, I think in the Ionic context, the correct thing to do is add the /// paths to the main.d.ts file. 因此,我认为在Ionic上下文中,正确的做法是将///路径添加到main.d.ts文件中。

The other question I had, ie how is the c3.js etc file included, this is all part of the Browserfity / gulp build, and this examines everything under the nodes_modules folder. 我还有另一个问题,即c3.js等文件是如何包含的,这是Browserfity / gulp构建的全部内容,并且检查了node_modules文件夹下的所有内容。 They all have the packeage.json file, and, in the case of c3, we have the line "main": "c3.js" . 它们都具有packeage.json文件,对于c3,我们具有"main": "c3.js"

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

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